[Tutor] basic lists and loops question

Jon Crump jjcrump at myuw.net
Thu May 15 00:03:49 CEST 2008


Bob, and Kent, Many thanks!

> Sounds like the key 'processed' is created by the assignment x['processed'] = 
> True. So those dictionaries that have not experienced this assignment have no 
> such key. You should instead use: if  'processed' in x:

Doh! Now that WAS obvious

> Try lst.remove(x)

Now this was odd. My trusty guide "Learning Python" (Lutz & Ascher) 2nd 
Ed. for python 2.3 says nothing about remove(). This must be 2.4 or 2.5 
yes?

this now has the desired effect (remains to be seen if it works for all 
instances):

def events(data):
   evts = []
   for x in data:
     for y in data:
       if (x['placename'] == y['placename']) and (x['end'].month + 1 == 
y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)):
         x['end'] = y['end']
         data.remove(x)
     evts.append(x)
   return evts

I understand about removing elements from a container you're iterating. Is 
data.remove(x) problematic in this context?



More information about the Tutor mailing list