iterating lists

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sat Jan 23 18:29:33 CET 2010


Op 2010-01-23 17:29, ceciliaseidel at gmx.de schreef:
> 
> 
> Arnaud Delobelle schrieb:
>> ceciliaseidel at gmx.de writes:
>>
>>> As you were talking about list.pop()...
>>>
>>> Is anyone able to reproduce the following and explain why this happens
>>> by chance? (Using 3.1.1)
>>>
>>> l1 = ["ready", "steady", "go"]
>>> l2 = ["one", "two", "tree"]
>>> l3 = ["lift off"]
>>>
>>> for w in l1:
> 
> Ouch... thanks Arnaud... The stable way would've been
> 
> for w in l1[:]: #use copy of l1 for iteration
> 	print(l1.pop()) #decomposite list

I would prefer:

while l1:
    print(l1.pop())

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven



More information about the Python-list mailing list