De-tupleizing a list

Algis Kabaila akabaila at pcug.org.au
Wed Apr 27 15:31:18 EDT 2011


On Wednesday 27 April 2011 20:56:20 John Pinner wrote:
> 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
> --
John,

That really tickles me - full marks to you!

OldAl.
-- 
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf



More information about the Python-list mailing list