[Tutor] basic lists and loops question
Jon Crump
jjcrump at myuw.net
Thu May 15 04:25:43 CEST 2008
Kent,
On Wed, 14 May 2008, Kent Johnson wrote:
>> I understand about removing elements from a container you're iterating. Is
>> data.remove(x) problematic in this context?
>
> Yes. It can cause the iteration to skip elements ofthe list. Better to
> post-process the list with a list comprehension:
> evts = [ evt for evt in evts if 'processed' not in evt ]
Thanks very much. This became clear when I applied the .remove(x) solution
to the whole list: very strange results. Altering x in the y loop also had
unforseen effects. I finally decorated every dictionary acted upon so I
could check everything that was going on. the appended merged dict got a
"merged" key and the two dictionaries that were merged each got a
"processed" key, then I post processed the list to remove the 'processed'
dictionaries **whew**. Thanks for clarifying AND saving me from error.
Jon
More information about the Tutor
mailing list