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

Loss of precision while converting floats to strings in pandas
PySide application crashes when setting a new widget to QScrollArea
Python - Download File Created From ASPX Form Submission
Find number of weeks in a month
breaking up a list entry in several lists at every /n Python 3
How can I get the default colors in GTK?
Ndb entry .put() not executed?
Tensorflow: NaNs propagating throughout network, even though using sparse_softmax etc
How to do a Python argparse mutually required argument group
scaling websocket game application server
Is there a way to find a character's Unicode code point in Python 2.7?
figtext datetime function matplotlib
environment variable in ubuntu
Cross-platform Python Executables
Extract subarray from collection of 2D coordinates?
tkinter populate treeview using threading pool

Categories

HOME
python
rest
cakephp-3.x
dotnetrdf
ssas-2012
jenkins-plugins
udp
elixir
tinyos
nuxeo
firebase-app-indexing
label
command
orientation
tizen-wearable-sdk
nodemailer
pca
wicket
jboss7.x
local
future
eclipse-luna
bootstrap-popover
flat-file
clover
plsqldeveloper
excel-2010
google-maps-android-api-2
onsen-ui
jquery-scrollify
fabric
non-deterministic
image-quality
reply
winscp
pcre
helix-3d-toolkit
outsystems
shapes
web-audio-api
fabric-digits
windows-10-iot-core
nunit-3.0
cgo
web-development-server
paho
pytest-django
filepath
3scale
openweathermap
timesten
broadcast
pinvoke
decompress
qos
rxvt
barcode-printing
sql-server-administration
ebtables
picasa
vst
wcf-ria-services
database-backups
com-interop
yoothemes
asp.net-mvc-2
approval-tests
hateoas
persistence.xml
abas
mptcp
adobe-indesign
google-hadoop
multipleselection
ticket-system
microblaze
collabnet
genetic-programming
csplit
system.web
adaptive-compression
dbsetup
unison
bounds
ksoap2
flash-cc
db4o
visual-studio-addins
sql-server-2012-web
angularjs-timeout
cgpath
mozart-mvc
code-conversion
jpf
cross-domain-policy
electronic-signature
galaxy-tab
printing-web-page
phpcrawl
libavformat
yajl
jquery-ui-button
openvg
hardware-infrastructure

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