De-tupleizing a list
Algis Kabaila
akabaila at pcug.org.au
Tue Apr 26 14:30:01 EDT 2011
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_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.
--
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf
More information about the Python-list
mailing list