De-tupleizing a list

John Pinner funthyme at gmail.com
Wed Apr 27 06:56:20 EDT 2011


On Apr 26, 4:28 am, Gnarlodious <gnarlodi... at gmail.com> 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 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
--




More information about the Python-list mailing list