how to flatten one level of list of lists?

Malcolm Tredinnick malcolm at commsecure.com.au
Tue May 29 20:29:06 CEST 2001


On Tue, May 29, 2001 at 01:36:12PM -0400, George Young wrote:
> I have a list like:
>   l = [[2], [3], [5], [11]]
> and I want to get:
>   ll = [2, 3, 5, 11]
> 
> I know I can do something like:
>   ll = map(lambda i: i[0],l) 
> 
> but it seems like overkill to have to use a lamba for such a simple
> task.
> Is there some easy way I'm missing?  

You mention you are using Python 2.1, so list cmoprehensions to the
rescue! :-)

ll = [x[0] for x in l]

Cheers,
Malcolm

-- 
How many of you believe in telekinesis? Raise my hand...




More information about the Python-list mailing list