On 23 October 2012 12:07, Jean-Michel Pichavant <jeanmic...@sequans.com>wrote:

> ----- Original Message -----
>
> > Thankyou.. but my problem is different than simply joining 2 lists
> > and it is done now :)....
>
>
> A lot of people though you were asking for joining lists, you description
> was misleading.
>
> I'll take a guess: you want to flatten a list of list.
> "Nested" list comprehensions can do the trick.
>
> aList =[[1,5], [2,'a']]
> [item for sublist in aList for item in sublist]
>
> ...
> [1, 5, 2, 'a']
>
> I find it rather difficult to read though.


We have a library function for this, in the one-and-only itertools.

>>> listoflists = [list(range(x, 2*x)) for x in range(5)]
> >>> listoflists
> [[], [1], [2, 3], [3, 4, 5], [4, 5, 6, 7]]
> >>> from itertools import chain
>  >>> list(chain.from_iterable(listoflists))
> [1, 2, 3, 3, 4, 5, 4, 5, 6, 7]


It does exactly what it says... fast and easy-to-read.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to