How to iterate on a changing dictionary
MRAB
python at mrabarnett.plus.com
Tue Jun 21 10:15:13 EDT 2011
On 21/06/2011 12:51, Gurpreet Singh wrote:
> 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
Using regex is overkill. Try this instead:
if "a" in v:
More information about the Python-list
mailing list