De-tupleizing a list
rustompmody at gmail.com
Tue Apr 26 01:26:55 EDT 2011
On Apr 26, 9:59 am, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Mon, 25 Apr 2011 20:28:22 -0700, Gnarlodious wrote:
> > 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
>>> l = [('0A',), ('1B',), ('2C',), ('3D',)]
>>> l_gen = (x for (x,) in l)
['0A', '1B', '2C', '3D']
More information about the Python-list