Kindly show me a better way to do it

Lie Ryan lie.1296 at gmail.com
Sun May 9 08:52:55 EDT 2010


On 05/09/10 19:18, Steven D'Aprano wrote:
> 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? 

one more nested tab. That extra whitespaces is quite irritating.

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?

You misunderstood. Tycho Anderson posted an itertools.chain(*chain)
solution for which Gunther Dietrich remarked "why not a nested loop"; I
am replying to Gunther Dietrich's nested loop with "because nested loop
is more complex than chain()" and added that the original[Tycho
Anderson's] chain solution has a subtle bug when facing infinite
generator of iterables.

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



More information about the Python-list mailing list