I try to remove all values 'x'from a large dictionary and DO NOT delete any keys, but when I run the code, the 'x'values still remain .
Here is an excerpt from my dictionary:
myDict = {0: ['id1', 'id2', 'id3'], 1: ['id1', 'id2', 'x', 'x'], 2: ['id1', 'x', 'x', 'x']}
My code is:
for k, v in myDict.iteritems():
if v == 'x':
myDict.remove('x')
print myDict
What am I striving for:
myDict = {0: ['id1', 'id2', 'id3'], 1: ['id1', 'id2'], 2: ['id1']}
How can I remove the values 'x'in the lists, please?
source
share