Kindly show me a better way to do it

Tycho Andersen tycho at tycho.ws
Sat May 8 17:00:47 EDT 2010


On Sat, May 8, 2010 at 3:41 PM, Oltmans <rolf.oltmans at gmail.com> wrote:
> Hi, I've a list that looks like following
>
> a = [ [1,2,3,4], [5,6,7,8] ]
>
> Currently, I'm iterating through it like
>
> for i in [k for k in a]:
>        for a in i:
>                print a
>
> but I was wondering if there is a shorter, more elegant way to do it?

How about itertools? In python 2.6:

>>> a = [ [1,2,3,4], [5,6,7,8] ]
>>> from itertools import chain
>>> for i in chain(*a):
...     print i
...
1
2
3
4
5
6
7
8



More information about the Python-list mailing list