Python

copy() method in dictionary

Click for All Topics

copy() method in dictionary - Python

  • This method is used to create a shallow copy of a dictionary.
  • The .copy() method doesn’t take any argument.
  • It returns a copy of a dictionary.
  • When we call this method with a dictionary it will create a new dictionary in the backend and iterate over the items of the original dictionary and every item will be appended to the new dictionary, finally it will return a newly copied dictionary.
  • It returns a shallow copy of a dictonary which means when we modify the original dictionary it will not affect the copied dictionary and vice versa.
  • The time complexity of this method is O(n) where n is the number of items in original dictionary

Syntax –

dictionary.clear()

Parameters –

  • argument → Doesn’t take any argument
  • return → return a shallow copy of a dictionary

Example  1 –

				
					my_dict={'one':1,'two':2,'three':3,'four':4,'five':5}
print("Original dictionary:\n",my_dict)

copied_dictionary=my_dict.copy()
print("Copied dictionary:\n",copied_dictionary)

# Output -
# Original dictionary:
# {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
# Copied dictionary:
# {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
				
			
  • In the above example, first we create a dictionary with some items and store it in my_dict variable.
  • After creating my_dict print the original dictionary on the output screen.
  • Then create a copy of the original dictionary using the .copy() method and store the copied dictionary in the copied_dictionary variable.
  • Finally, print the copied dictionary on the output screen.

Example 2- Let’s see if modifying the original dictionary affects the copied dictonary or not.

				
					dct={'vowels':['a','e','i','o','u'],'hello':'world!'}
print("Original Dictionary:\n",dct)

dct_copy=dct.copy()
print("Copied dictionary:\n",dct_copy)
dct['hello']='Python'
print("Modify original dictionary:\n",dct)
print("Copied dictionary:\n",dct_copy)

# Output -
# Original Dictionary:
# {'vowels': ['a', 'e', 'i', 'o', 'u'], 'hello': 'world!'}
# Copied dictionary: 
# {'vowels': ['a', 'e', 'i', 'o', 'u'], 'hello': 'world!'}
# Modify original dictionary: 
# {'vowels': ['a', 'e', 'i', 'o', 'u'], 'hello': 'Python'}
# Copied dictionary: 
# {'vowels': ['a', 'e', 'i', 'o', 'u'], 'hello': 'world!'}
				
			

Example 3- create a copy of a dictionary without using an in-built method e.g. .copy()

				
					student = {
    "name": "ABC",
    "age": 20,
    "major": "Computer Science",
    "grades": [90, 85]
}
new_dict={}
for key, value in student.items():
    new_dict[key] = value
print("Original dictionary:\n",student)
print("Copied dictionary:\n",new_dict)

# Output -
# Original dictionary:
# {'name': 'ABC', 'age': 20, 'major': 'Computer Science', 'grades': [90, 85]}
# Copied dictionary:
# {'name': 'ABC', 'age': 20, 'major': 'Computer Science', 'grades': [90, 85]}
				
			

Example –

				
					my_dict={'A':10,'B':20,'C':30,'D':40,'E':50}
my_dict1=my_dict1
print("Original dictionary:\n",my_dict)
print("Copied dictionary:\n"my_dict1)
my_dict.update({'F':60})
print("\nModified original dictionary:\n",my_dict)
print("Copied dictionary:\n",my_dict1)

# Output -
# Original dictionary:
# {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}
# Copied dictionary: 
# {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}

# Modified original dictionary: 
# {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50, 'F': 60}
# Copied dictionary: 
# {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50, 'F': 60}
				
			
  • In the above example, you can see that when we modify the original dictionary it modifies the copied dictionary as well.
  • This is because we do not create a copy of the original dictionary instead of creating a copy we create another reference of the original dictionary object that’s why it happens.
  • In this situation, if we modify any dictionary the changes will affect both the dictionary.