Converting the keys of a dictionary from numeric to string
pozz
pozzugno at gmail.com
Wed Oct 19 20:34:45 EDT 2016
I have a dictionary where the keys are numbers:
mydict = { 1: 1000, 2: 1500, 3: 100 }
I would like to convert keys from number to string representation:
mydict = { "apples": 1000, "nuts": 1500, "tables": 100 }
Of course, somewhere I have the association between key-numbers and
key-strings, maybe in another dictionary:
keydict = { 1: "apples", 2: "nuts", 3; "tables" }
How could I make the conversion? My solution:
keydict = { 1: "Joseph", 2: "Michael", 3: "John" }
mydict = { 1: 1000, 2: 2000, 3: 3000 }
newdict = {}
for k in mydict.keys():
newdict.update({keydict[k]: mydict[k]})
print(newdict)
I tried changing just mydict (without creating newdict), without success.
More information about the Python-list
mailing list