De-tupleizing a list

G

Gnarlodious

I have an SQLite query that returns a list of tuples:

[('0A',), ('1B',), ('2C',), ('3D',),...

What is the most Pythonic way to loop through the list returning a
list like this?:

['0A', '1B', '2C', '3D',...

-- Gnarlie
 
P

Philip Semanchuk

I have an SQLite query that returns a list of tuples:

[('0A',), ('1B',), ('2C',), ('3D',),...

What is the most Pythonic way to loop through the list returning a
list like this?:

['0A', '1B', '2C', '3D',...


This works for me -

result = [('0A',), ('1B',), ('2C',), ('3D',), ]
result = [row[0] for row in result]


Cheers
Philip
 
C

CM

I have an SQLite query that returns a list of tuples:

[('0A',), ('1B',), ('2C',), ('3D',),...

What is the most Pythonic way to loop through the list returning a
list like this?:

['0A', '1B', '2C', '3D',...

-- Gnarlie

For just this case, if returned_list is your list above, how about?:

flat_list = [item[0] for item in returned_list]
 
P

Paul Rubin

Gnarlodious said:
I have an SQLite query that returns a list of tuples:
[('0A',), ('1B',), ('2C',), ('3D',),...
What is the most Pythonic way to loop through the list returning a
list like this?:
['0A', '1B', '2C', '3D',...

Try:

tlist = [('0A',), ('1B',), ('2C',), ('3D',)]
alist = [x for (x,) in tlist]
 
J

John Connor

itertools can help you do this too:

import itertools
tl = [('0A',), ('1B',), ('2C',), ('3D',)]

itertools.chain.from_iterable(tl)
<itertools.chain object at 0x11f7ad0>

list(itertools.chain.from_iterable(tl))
['0A', '1B', '2C', '3D']


Checkout http://docs.python.org/library/itertools.html#itertools.chain
for more info.

Gnarlodious said:
I have an SQLite query that returns a list of tuples:
[('0A',), ('1B',), ('2C',), ('3D',),...
What is the most Pythonic way to loop through the list returning a
list like this?:
['0A', '1B', '2C', '3D',...

Try:

   tlist = [('0A',), ('1B',), ('2C',), ('3D',)]
   alist = [x for (x,) in tlist]
 
S

Steven D'Aprano

I have an SQLite query that returns a list of tuples:

[('0A',), ('1B',), ('2C',), ('3D',),...

What is the most Pythonic way to loop through the list returning a list
like this?:

['0A', '1B', '2C', '3D',...

Others have pointed you at a list comprehension, but just for completion,
there's also this:

from operator import itemgetter
map(itemgetter(0), list_of_tuples)

In Python 3, map becomes lazy and returns an iterator instead of a list,
so you have to wrap it in a call to list().
 
R

rusi

I have an SQLite query that returns a list of tuples:
[('0A',), ('1B',), ('2C',), ('3D',),...
What is the most Pythonic way to loop through the list returning a list
like this?:
['0A', '1B', '2C', '3D',...

Others have pointed you at a list comprehension, but just for completion,
there's also this:

from operator import itemgetter
map(itemgetter(0), list_of_tuples)

In Python 3, map becomes lazy and returns an iterator instead of a list,
so you have to wrap it in a call to list().

Going the other way: Given that most lists are processed and discarded
you can stop at the second step.

[A 3-way overloading of '()' here that may be a bit unnerving at
first...]

l = [('0A',), ('1B',), ('2C',), ('3D',)]
l_gen = (x for (x,) in l)
list(l_gen)
['0A', '1B', '2C', '3D']
 
G

Gnarlodious

In Python 3, map becomes lazy and returns an iterator instead of a list,
so you have to wrap it in a call to list().

Ah, thanks for that tip. Also works for outputting a tuple:
list_of_tuples=[('0A',), ('1B',), ('2C',), ('3D',)]

#WRONG:
(x for (x,) in list_of_tuples)
<generator object <genexpr> at 0x1081ee0>

#RIGHT:
tuple(x for (x,) in list_of_tuples)

Thanks everyone for the abundant help.

-- Gnarlie
 
A

Algis Kabaila

In Python 3, map becomes lazy and returns an iterator
instead of a list, so you have to wrap it in a call to
list().

Ah, thanks for that tip. Also works for outputting a tuple:
list_of_tuples=[('0A',), ('1B',), ('2C',), ('3D',)]

#WRONG:
(x for (x,) in list_of_tuples)
<generator object <genexpr> at 0x1081ee0>

#RIGHT:
tuple(x for (x,) in list_of_tuples)

Thanks everyone for the abundant help.

-- Gnarlie

I think you already have the following:
t = [('0A',), ('1B',), ('2C',), ('3D',)]
ls = [v[0] for v in t]
ls ['0A', '1B', '2C', '3D']

I would prefer that to using a ready made module, as it would
be quicker than learning about the module, OTH, learning about
a module may be useful for other problems. A standard dilema...

The above quote of code is in Idle3, running Python 3.1.

OldAl.
 
C

Chris Angelico

(2)    return [item[0] for item in lst]                          #relative time:  106
(5)    return [x for (x,) in lst]                                     #relative time:  52

Interesting indeed. #5 will of course only work with a tuple of length
1, where most of the others are flexible enough to take the first
element from any length tuple; but the time saving is quite
significant.

Chris Angelico
 
H

Hans Georg Schaathun

I would prefer that to using a ready made module, as it would
: be quicker than learning about the module, OTH, learning about
: a module may be useful for other problems. A standard dilema...

More importantly, list comprehension is very readable to /other/
people. I don't know exactly what the pythonic philosopy is,
but when I started using python, more readable and intuitive code
was one of the main motivators, and the only one which favours
python both over C/java and over Matlab ...

List comprehension is understood even by readers with no experience
with python.
 
E

Ethan Furman

Hans said:
List comprehension is understood even by readers with no experience
with python.

There's nothing magically understandable about a list comp -- the first
time I saw one (which was in Python), I had to learn about them.

~Ethan~
 
H

Hans Georg Schaathun

Hans Georg Schaathun wrote:
: > List comprehension is understood even by readers with no experience
: > with python.
:
: There's nothing magically understandable about a list comp -- the first
: time I saw one (which was in Python), I had to learn about them.

Well, there is a first time for everything.
For all the other proposals, the first time is bound to be in python.

List comprehension is found in many languages, as just list
comprehension, and rather quickly comprehensible by analogy
to anyone familiar with set comprehension in mathematics.
The syntax is slightly different, but python's use of plain
English keywords make the transission fairly simple.

(-: and I did not say by /all/ readers :)
 
R

Raymond Hettinger

J

John Pinner

I have an SQLite query that returns a list of tuples:

[('0A',), ('1B',), ('2C',), ('3D',),...

What is the most Pythonic way to loop through the list returning a
list like this?:

['0A', '1B', '2C', '3D',...

-- Gnarlie

If you want to handle a list of tuples where each tuple could have
*more* than one element, one solution would be:
L = [(1, 2), (2, 3, 4), (5,), (6, 7, 8, 9, 0)]
tuple([ x for t in L for x in t ]) (1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 0)

John
--
 
A

Algis Kabaila

I have an SQLite query that returns a list of tuples:

[('0A',), ('1B',), ('2C',), ('3D',),...

What is the most Pythonic way to loop through the list
returning a list like this?:

['0A', '1B', '2C', '3D',...

-- Gnarlie

If you want to handle a list of tuples where each tuple could
have

*more* than one element, one solution would be:
L = [(1, 2), (2, 3, 4), (5,), (6, 7, 8, 9, 0)]
tuple([ x for t in L for x in t ])

(1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 0)


John
--
John,

That really tickles me - full marks to you!

OldAl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top