Python change all keys of a dictionary
Question:
How do I change all keys of a dictionary
code:
d1 = {'mango':10,
'bananna':20,
'grapes':30}
d1_new_keys = ['yellow mango','green banana','red grapes']
d1.keys() = d1_new_keys
present answer:
SyntaxError: can't assign to function call
expected answer:
d1 = {'yellow mango': 10, 'green bananna': 20, 'red grapes': 30}
Answers:
What you’re doing is trying to set the function d1.keys()
to a list thus the error. The best way to do this would be with dictionary comprehension:
d1 = {k: v for (k, v) in zip(d1_new_keys, d1.values()}
This would essentially create a new dictionary and set it equal to d1 where the keys come from the d1_new_keys
list and the values come from the original d1
Mixing my answer with the others, a shorter solution would be.
d1_new_keys must have all the keys.
d1 = {'mango':10, 'bananna':20, 'grapes':30}
d1_new_keys = {
'mango': 'yellow mango',
'bananna': 'green banana',
'grapes':'red grapes' }
d1 = {d1_new_keys[k]: v for k, v in d1.items()}
Original answer:
Dictionaries are not ordered lists, so you can’t just change the keys.
What you have to do, is build a new dictionary with the new keys.
d1 = {'mango':10, 'bananna':20, 'grapes':30}
d1_new_keys = {
'mango': 'yellow mango',
'bananna': 'green banana',
'grapes':'red grapes' }
d2 = {}
for d in d1:
if d in d1_new_keys:
d2[d1_new_keys[d]] = d1[d]
d1 = d2
To update the dict (without creating a new one):
d1 = {'mango':10, 'bananna':20, 'grapes':30}
d1_current_keys = list(d1.keys())
d1_new_keys = ['yellow mango','green banana','red grapes']
for current_key_ix, new_key in enumerate(d1_new_keys):
d1[new_key] = d1.pop(d1_current_keys[current_key_ix])
NOTE: beware of depending on the order of the keys.
How do I change all keys of a dictionary
code:
d1 = {'mango':10,
'bananna':20,
'grapes':30}
d1_new_keys = ['yellow mango','green banana','red grapes']
d1.keys() = d1_new_keys
present answer:
SyntaxError: can't assign to function call
expected answer:
d1 = {'yellow mango': 10, 'green bananna': 20, 'red grapes': 30}
What you’re doing is trying to set the function d1.keys()
to a list thus the error. The best way to do this would be with dictionary comprehension:
d1 = {k: v for (k, v) in zip(d1_new_keys, d1.values()}
This would essentially create a new dictionary and set it equal to d1 where the keys come from the d1_new_keys
list and the values come from the original d1
Mixing my answer with the others, a shorter solution would be.
d1_new_keys must have all the keys.
d1 = {'mango':10, 'bananna':20, 'grapes':30}
d1_new_keys = {
'mango': 'yellow mango',
'bananna': 'green banana',
'grapes':'red grapes' }
d1 = {d1_new_keys[k]: v for k, v in d1.items()}
Original answer:
Dictionaries are not ordered lists, so you can’t just change the keys.
What you have to do, is build a new dictionary with the new keys.
d1 = {'mango':10, 'bananna':20, 'grapes':30}
d1_new_keys = {
'mango': 'yellow mango',
'bananna': 'green banana',
'grapes':'red grapes' }
d2 = {}
for d in d1:
if d in d1_new_keys:
d2[d1_new_keys[d]] = d1[d]
d1 = d2
To update the dict (without creating a new one):
d1 = {'mango':10, 'bananna':20, 'grapes':30}
d1_current_keys = list(d1.keys())
d1_new_keys = ['yellow mango','green banana','red grapes']
for current_key_ix, new_key in enumerate(d1_new_keys):
d1[new_key] = d1.pop(d1_current_keys[current_key_ix])
NOTE: beware of depending on the order of the keys.