Reputation: 305
{"green": 0, "y3": 1, "m@tt": 0, "newaccount": 0, "egg": 0, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}
this is the dictionary defined as 'completeddict'. What I want to do, is to change ALL values no matter what they are called to 0. However bear in mind that new account names will be added at any point as 'keys' so I cannot manually do "completeddict[green] = 0", "completeddict[lewis] = 0", etc etc.
Is there any way to have python change ALL values within a dictionary back to 0?
EDIT: Unfortunately I do not want to create a new dictionary - I want to keep it called 'completeddict' as the program needs to use the dictionary defined as 'completeddict' at many points in the program.
Upvotes: 13
Views: 47823
Reputation: 7335
I had a similar situation where the dictionary could itself have nested dictionary and list values and all values that were not of type dictionary/list needed to be set to a default value:
def Clear(e1,defaultValue):
if type(e1) is dict:
return {k:Clear(e1[k], defaultValue) for k in e1.keys()}
elif type(e1) is list:
return [Clear(v,defaultValue) for v in e1]
else:
return defaultValue
Upvotes: 0
Reputation: 474131
Another option is to use .fromkeys()
:
fromkeys(seq[, value])
Create a new dictionary with keys from seq and values set to value.
d = d.fromkeys(d, 0)
Demo:
>>> d = {"green": 0, "y3": 1, "m@tt": 0, "newaccount": 0, "egg": 0, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}
>>> d.fromkeys(d, 0)
{'newaccount': 0, 'swag': 0, 'notin': 0, 'NewAccount1': 0, 'Matt1': 0, 'testyear4': 0, 'Lewis': 0, 'dan': 0, 'matt': 0, 'results': 0, 'm@tt': 0, 'green': 0, 'testyear5': 0, 'lewis': 0, 'NewAccount2': 0, 'y3': 0, 'testyear3': 0, 'egg': 0, 'testyear6': 0}
Upvotes: 44
Reputation: 21
Adding to alecxe asnwer,
your dictionary is,
completeddict = {"green": 0, "y3": 1, "m@tt": 0, "newaccount": 0, "egg": 0, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}
now this dictionary can be updated with default values,
completeddict.update({}.fromkeys(completeddict,0)
Done. your completeddict is updated with default value for all keys.
Ex:
a = {'x':2,'y':4}
print id(a)
print a
a.update({}.fromkeys(a,0))
print id(a)
print a
Output:
42612608 <--- id of dict 'a'
{'y': 4, 'x': 2} <--- dict 'a' before updation
42612608 <--- id of dict 'a'
{'y': 0, 'x': 0} <--- dict 'a' after updation
It can be observed that id of the dictionary same even after it is updated with default values.
Upvotes: 2
Reputation: 386295
If you don't want to create a new dict, it seems like all you need is a simple two-line loop, unless I'm missing something:
for key in completeddict.keys():
completeddict[key] = 0
Upvotes: 7
Reputation: 104072
A form that will work in virtually any Python version is just use the dict
constructor with a list comprehension or generator:
>>> d={"green": 10, "y3": 11, "m@tt": 12, "newaccount": 22, "egg": 55, "results": 0, "dan": 0, "Lewis": 0, "NewAccount2": 0, "testyear3": 1, "testyear6": 0, "NewAccount1": 0, "testyear4": 0, "testyear5": 0, "Matt1": 0, "swag": 1, "lewis": 1, "matt": 1, "notin": 0}
>>> d=dict((k, 0) for k in d)
>>> d
{'newaccount': 0, 'swag': 0, 'notin': 0, 'NewAccount1': 0, 'Matt1': 0, 'testyear4': 0, 'Lewis': 0, 'dan': 0, 'matt': 0, 'results': 0, 'm@tt': 0, 'green': 0, 'testyear5': 0, 'lewis': 0, 'NewAccount2': 0, 'y3': 0, 'testyear3': 0, 'egg': 0, 'testyear6': 0}
Which you can then use with a mutable argument or some form of callable:
>>> d=dict((k, list()) for k in d)
Now each element of d
is a new list (or whatever else mutable or not you would want to set the value portion of the tuple to be).
Upvotes: 5
Reputation: 5534
Sure, its pretty easy:
newDict = {key: 0 for key in completeddict}
This is dictionary comprehension and is equivalent to:
newDict = {}
for key in completeddict:
newDict[key] = 0
These dict comprehensions were introduced in v2.7, so older vesions will need to use the expanded form.
Upvotes: 15