bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python - Copy Dictionaries

Concept visual

Python - Copy Dictionaries

keys map to buckets01kiwi:12pear:43apple:7grape:9

Copy a Dictionary

You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a to dict1, and changes made in dict1 will automatically also be made in dict2.

Formula

There are ways to make a copy, one way is to use the built - in Dictionary method copy().

Example

Make a copy of a dictionary with the copy() method:

thisdict =  {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

mydict = thisdict.copy()

print(mydict)

Formula

Another way to make a copy is to use the built - in function dict().

Example

Make a copy of a dictionary with the dict() function:

thisdict =  {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

mydict = dict(thisdict)

print(mydict)

Previous

Python - Loop Dictionaries

Next

Python Dictionary Methods