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

Single player 'pong' game
Facebook API non-interactive authorization/login
__getattr__ keeps returning None even when I attempt to return values
python foreign character in csv
How to pack python files and its dependencies in a single executable file?
Printing Variable names and contents as debugging tool; looking for emacs/Python shortcut
Cheking added file to upload python, pylons?
How to refer to the local module in Python?
Is close() necessary when using iterator on a Python file object [duplicate]
Django Admin “Edit Selection” Action?
How to change firefox proxy from webdriver?
Is it possible to hook up a more robust HTML parser to Python mechanize?
Enable Unicode “globally” in Python
Dynamically import a callable given the full module path?
python chaining
py2app and xml.etree.ElementTree

Categories

HOME
makefile
app-inventor
openssl
deep-linking
mapbox-gl-js
bibtex
is-empty
httpclient
dropbox
data-synchronization
cpanel
jacoco
medium-editor
zope
bar-chart
redux-observable
grub2
pyephem
jogl
vs2017
koa
marathon
ghost-inspector
spring-ws
header-files
tflearn
branch
cep
excel-2013
google-api-dotnet-client
fileinfo
autofill
pep8-assembly
jackson-dataformat-csv
pdflatex
idl
google-drive-realtime-api
large-data
superpowered
office365connectors
elfinder
topic-modeling
concrete5-5.7
wcf-security
floating-accuracy
wsadmin
microdata
android-maps-v2
floor
nsjsonserialization
taskmanager
ti-basic
dwarf
tofixed
vici
pdfkit
coremidi
icefaces
activeweb
mongoose-populate
mailcatcher
gitweb
try-finally
zurb-foundation-apps
unsatisfiedlinkerror
kendo-combobox
financial
concur
aerogear
lemoon
direct3d12
persistence.xml
android-viewholder
file-diffs
code-readability
android-snackbar
connection-reset
gray-code
web-component-tester
hiera
cpan
alphablending
line-numbers
grunt-contrib-concat
dmp
node-inspector
collabnet
genetic-programming
method-overriding
eclipse-classpath
emma
sgml
gfs
pyjade
real-time-updates
fogbugz-api
trimming
ng-hide
entity-framework-4.1
lame
fileconveyor
fortran77
xgettext
unions
path-manipulation
coff
msbuildextensionpack
noir
for-xml-path
graph-layout

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