Kindly show me a better way to do it

Lie Ryan lie.1296 at gmail.com
Sun May 9 01:17:38 EDT 2010


On 05/09/10 07:09, Günther Dietrich wrote:
> 
> 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?

IMHO that's more complex due to the nested loop, though I would
personally do it as:

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

so it won't choke when 'a' is an infinite stream of iterables.



More information about the Python-list mailing list