Re: De-tupleizing a list

2011-04-27 Thread Algis Kabaila
On Wednesday 27 April 2011 20:56:20 John Pinner wrote: > On Apr 26, 4:28 am, 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?

Re: De-tupleizing a list

2011-04-27 Thread John Pinner
On Apr 26, 4:28 am, 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',... > > -- Gnarlie If you want to handle

Re: De-tupleizing a list

2011-04-26 Thread Raymond Hettinger
On Apr 25, 8:28 pm, 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',... You could unpack the 1-tuple the sam

Re: De-tupleizing a list

2011-04-26 Thread Hans Georg Schaathun
On Tue, 26 Apr 2011 13:37:40 -0700, Ethan Furman wrote: : 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),

Re: De-tupleizing a list

2011-04-26 Thread Ethan Furman
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. ~Ethan~ -- http://mail.python.org/mailman/li

Re: De-tupleizing a list

2011-04-26 Thread Hans Georg Schaathun
On Wed, 27 Apr 2011 04:30:01 +1000, Algis Kabaila wrote: : 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 comprehensio

Re: De-tupleizing a list

2011-04-26 Thread Chris Angelico
On Wed, Apr 27, 2011 at 5:39 AM, Mark Niemczyk wrote: > (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 t

Re: De-tupleizing a list

2011-04-26 Thread Mark Niemczyk
Some interesting performance comparisons, under Python 3.2. Times are relative, and are for an initial list of tuples with 500,000 items. (1)ans = [] #relative time: 298 for item in lst: ans += list(item

Re: De-tupleizing a list

2011-04-26 Thread Algis Kabaila
On Tuesday 26 April 2011 22:19:08 Gnarlodious wrote: > On Apr 25, 10:59 pm, Steven D'Aprano wrote: > > 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

Re: De-tupleizing a list

2011-04-26 Thread Gnarlodious
On Apr 25, 10:59 pm, Steven D'Aprano wrote: > 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,)

Re: De-tupleizing a list

2011-04-25 Thread rusi
On Apr 26, 9:59 am, Steven D'Aprano 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?:

Re: De-tupleizing a list

2011-04-25 Thread Steven D'Aprano
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 point

Re: De-tupleizing a list

2011-04-25 Thread John Connor
itertools can help you do this too: import itertools tl = [('0A',), ('1B',), ('2C',), ('3D',)] itertools.chain.from_iterable(tl) list(itertools.chain.from_iterable(tl)) ['0A', '1B', '2C', '3D'] Checkout http://docs.python.org/library/itertools.html#itertools.chain for more info. On Mon, Apr

Re: De-tupleizing a list

2011-04-25 Thread Paul Rubin
Gnarlodious writes: > 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',)] al

Re: De-tupleizing a list

2011-04-25 Thread Philip Semanchuk
On Apr 25, 2011, at 11:28 PM, 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',... This works for me -

Re: De-tupleizing a list

2011-04-25 Thread Gnarlodious
On Apr 25, 9:42 pm, CM wrote: > flat_list = [item[0] for item in returned_list] HA! So easy. Thanks. -- Gnarlie -- http://mail.python.org/mailman/listinfo/python-list

Re: De-tupleizing a list

2011-04-25 Thread Littlefield, Tyler
>What is the most Pythonic way to loop through the list returning a >list like this?: here's how I'd do it: >>> i [(1, 'a'), (2, 'b'), (3, 'c')] >>> for item in i: ... a+=list(item) ... ... >>> a [1, 'a', 2, 'b', 3, 'c'] -- http://mail.python.org/mailman/listinfo/python-list

Re: De-tupleizing a list

2011-04-25 Thread CM
On Apr 25, 11:28 pm, 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',... > > -- Gnarlie For just this case,

De-tupleizing a list

2011-04-25 Thread 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 -- http://mail.python.org/mailman/listinfo/python-list