python


How to convert the value_list to list in Django


Hi I have the values_list as follows:
x = [(u'All',), (u'C4DI',), (u'C4MI',), (u'TBAZ_DI',), (u'TBAZ_FI',), (u'TTAZ_DI',), (u'TTAZ_FI',)]
How to convert this values_list to a normal list like
[Al, C4, C4, ...]
Any idea guys? I tried
y = list(x)
But that didnt worked. Guys how can I do that?
Since, you want only the first 2 characters in the list:
>>> x = [(u'All',), (u'C4DI',), (u'C4MI',), (u'TBAZ_DI',), (u'TBAZ_FI',), (u'TTAZ_DI',), (u'TTAZ_FI',)]
>>> [str(i[0][:2]) for i in x]
['Al', 'C4', 'C4', 'TB', 'TB', 'TT', 'TT']
EDIT: If you are using flat=True (As given in yedpodtrztiko's answer) then use the following code:
[str(i[:2]) for i in x]
Where x = [u'C4DI', u'C4MI', u'All', u'TTAZ_DI', u'TBAZ_DI', u'TTAZ_FI', u'TBAZ_FI']
use the parameter flat=True to get a flatten list straight away:
Model.objects.values_list('column', flat=True)
You can try this all so, here I am using str() which convert unicode to str
y = [str(a[0]) for a in x]
y
['All', 'C4DI', 'C4MI', 'TBAZ_DI', 'TBAZ_FI', 'TTAZ_DI', 'TTAZ_FI']
Just try this one
y = [str(a[0][:2]) for a in x]
You want first two character, then I just slice with 2
You can do like this using list comprehension:
x = [(u'All',), (u'C4DI',), (u'C4MI',), (u'TBAZ_DI',), (u'TBAZ_FI',), (u'TTAZ_DI',), (u'TTAZ_FI',)]
q = [str(i[0] + i[1]) for a in x for i in a]

Related Links

Turning .py file into exe [duplicate]
Using a callback function to find the sum of values in a bst (without a global)
Append numeric value to binary string
string not returned from function, but list is returned
Circus WEBUI is not working
'NoneType' error
Fully authorize a local application using OAuth for tumblr
Convert Data Objects to Strings for Script
Python 3 pyOpenGL install problems
Can I validate Azure credentials without generating Certificate for my machine?
How to return the multiple indexes of word using .index in Python
Scrape Multiple URLs using Beautiful Soup
Upload CSV in myBucket and Read File in S3 AWS using python
Docker, dockercloud and Cron
Flask - database items not being created with form submission
Customize help among groups in argparse

Categories

HOME
xamarin.android
jsviews
system-verilog
blockchain
mapping
actionscript
couchdb-2.0
sbt-assembly
value
bro
sequelize.js
structuremap
lotus-notes
packer
keystore
ndis
cortex-a
public-key-encryption
spring-cloud-config
ssms-2016
session-timeout
ng-show
prestodb
linq-to-sql
propel2
marathon
statusbar
esql
epicor
remove-method
philips-hue
interrupt-handling
classpath
helper
gzip
positioning
autofill
websauna
pep8-assembly
devforce
skygear
pljson
passenger
google-guava-cache
es-shell
xamarin.uitest
floating-accuracy
sendgrid-templates
taglib
resuming-training
amazon-clouddrive
archer
appstore-approval
crash-reports
rpostgresql
fedora20
opencpu
barcode-printing
pspice
atlassian-crowd
typhoon
freetype2
qregexp
ng2-material
flexjson
sun-codemodel
direct3d12
hornetq
sem
anti-patterns
castle-windsor-3
ffserver
scrollspy
jython-2.7
sqoop2
java-melody
fragment-tab-host
hidden-field
snmp4j
dalekjs
genetic-programming
dto
system.web
proxies
joox
bfd
snapjs
android-4.0
git-filter-branch
subtract
gjs
entity-framework-ctp5
flexicious
ubuntu-11.04
electronic-signature
surf
galaxy-tab
testunit
osx-leopard
sitemappath
camtasia
communicationexception
scala-2.8
aio
linfu
revision
rd

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App