Kindly show me a better way to do it

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun May 9 05:18:10 EDT 2010


On Sun, 09 May 2010 15:17:38 +1000, Lie Ryan wrote:

> 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, 

What's so complex about a nested loop? And why are you saying that it is 
"more complex" than the Original Poster's solution, which also had a 
nested loop, plus a pointless list comprehension?


> 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.

Neither will a nested for-loop.


-- 
Steven



More information about the Python-list mailing list