best way to do this

Hrvoje Niksic hniksic at xemacs.org
Tue Dec 2 15:36:01 CET 2008


TP <Tribulations at Paralleles.invalid> writes:

> Hi everybody,
>
>>>> c=[(5,3), (6,8)]
>
> From c, I want to obtain a list with 5,3,6, and 8, in any order.
> I do this:
>
>>>> [i for (i,j) in c] + [ j for (i,j) in c]
> [5, 6, 3, 8]
>
> Is there a quicker way to do this?

Quicker?  Hard to say.  Using itertools elegantly?  Definitely:

list(chain(*c))

As an added benefit, it works regardless of the number of elements in
the tuple.  (Also assumes from itertools import chain for effect.)



More information about the Python-list mailing list