[Tutor] quickie: a better dynamic dictionary update?

Marcus Goldfish magoldfish at gmail.com
Wed Jul 12 03:20:25 CEST 2006


Hi,

I need to keep an updated dictionary of pictures in my application.  I have
a function which takes snapshots of the current pictures available.  Some of
the pictures in my dictionary have been deleted, so their dict entry needs
to be removed.  Here is a snippet of very ugly code I have which works.  I'm
looking for quick suggestions for a better implementation-- all nice replies
welcomed!

Thanks,
Marcus

-- code snippet

# 1st, find the 'stale' items in our dictionary to delete
# lstKeepers is a list of current pictures
# Note: if I try to iterate over the keys of the dict and
# remove-as-I-go, I get an exception (dict size changed
# during iteration)
lstRemove = []
for key in myDict:
   if key not in lstKeepers:
       lstRemove.append(key)

# 2nd, remove them
for oldKey in lstRemove:
   del myDict[oldKey]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060711/20816b5d/attachment.htm 


More information about the Tutor mailing list