How to iterate on a changing dictionary

Gurpreet Singh gurpreetsingh13 at khalsa.com
Tue Jun 21 07:51:14 EDT 2011


Perhaps this is the simplest and best solution which appears in this case. Just copy the desired items to a new dictionary and discard the original one.

import re
myDict={'a':'alpha','b':'beta','c':'charley','d':'disney'}
myNewDict={}
for k,v in myDict.iteritems():
    if re.search("a",v)!=None:
       myNewDict[k]=v
print myDict
print myNewDict



More information about the Python-list mailing list