Answers for "dictinary change key"

4

change key of dictionary python

>>> dictionary = { 1: 'one', 2:'two', 3:'three' }
>>> dictionary['ONE'] = dictionary.pop(1)
>>> dictionary
{2: 'two', 3: 'three', 'ONE': 'one'}
>>> dictionary['ONE'] = dictionary.pop(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: 1
Posted by: Guest on June-10-2020
-2

python change dict key value

# an example of creating a new dict key from an existing one, then deleting the old key
d = {"cow": "animal", "rose": "flower"}
d["horse"] = d["cow"]
del d["cow"]
Posted by: Guest on April-20-2022

Code answers related to "dictinary change key"

Python Answers by Framework

Browse Popular Code Answers by Language