On Apr 26, 9:59 am, Steven D'Aprano <steve +comp.lang.pyt...@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 first...] >>> l = [('0A',), ('1B',), ('2C',), ('3D',)] >>> l_gen = (x for (x,) in l) >>> list(l_gen) ['0A', '1B', '2C', '3D'] -- http://mail.python.org/mailman/listinfo/python-list