Kindly show me a better way to do it
Günther Dietrich
gd.usenet at spamfence.net
Sat May 8 17:09:06 EDT 2010
Tycho Andersen <tycho at tycho.ws> wrote:
>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
Why not this way?
>>> a = [[1,2,3,4], [5,6,7,8]]
>>> for i in a:
... for j in i:
... print(j)
...
1
2
3
4
5
6
7
8
Too simple?
Best regards,
Günther
More information about the Python-list
mailing list