Python Dictionary Copy with Examples

In this Python tutorial, we will discuss the Python dictionary Copy. Here we will also cover the below examples:

  • Python dictionary copy vs deep copy
  • Python dictionary copy deep
  • Python dictionary copy method
  • Python dictionary copy by value
  • Python dictionary copy keys
  • Python dictionary copy and update
  • Python dictionary copy to list
  • Python copy dictionary without reference
  • Python copy dictionary to new variable

Python dictionary Copy

  • In Python, the dictionary copy() method is used to create a new dictionary and contains a copy of similar elements from the original dictionary.
  • This method always returns a shallow copy of the given dictionary and it will help the user to copy each and every element from the original dictionary and store them into a new dict. It does not update the original dictionary just returns a copy of the dictionary.
  • Let’s see how to copy elements from the original dictionary by using the copy() method.

Syntax:

Here is the Syntax of the dictionary copy() method

dict.copy()

Note: This method does not take any parameter and returns a shallow copy of the dictionary.

Example:

country_nam = {'Australia':17, 'Germany':18, 'Cananda':81, 'England':15, 'China':77 }

cpy_dict = country_nam.copy()
print("old dictionary: ",country_nam)
print("New dictionary: ",cpy_dict)

In the above code first, we will initialize a dictionary ‘country_nam’ with some key-value pair elements and then use the copy() method on it and create a new shallow copy.

Here is the execution of the following given code

Python dictionary copy
Python dictionary copy

Read Python Dictionary Methods + Examples

How to copy elements from the original dictionary

By using the ‘=’ operator method we can perform this particular task. In Python, the ‘=’ operator is used to create a new dictionary and copies references of each object available in the original dictionary.

In this example, if any update is made in the copied dictionary then it will also reflect in the old dictionary.

Source Code:

my_dict = {56:'n', 76:'m'}

cp_dict = my_dict 
cp_dict.clear()
print('copy dictionary: ', cp_dict)
print('old dictionary: ', my_dict)

In the above code first, we initialize a dictionary ‘my_dict’ and copy it to a new dictionary that is ‘cp_dict’. By using ‘cp_dict= my_dict’. This operator copies each element present in my_dict and returns it into a new dictionary.

Now use the clear() method to remove all the elements from the dictionary it will display both the dictionaries are empty.

Here is the Screenshot of the following given code

Python dictionary copy
Python dictionary copy

Read: Python dictionary multiple keys

Python dictionary Copy vs deep copy

Let us see what is Python dictionary Copy vs deep copy?

  • In Python dictionary, the shallow copy means a new collection object which contains the reference of the original elements and it creates a new instance and the values are copied into the new instance. While in the case of shallow copy the process of copying does not iterate and could not declare copies of the child objects.
  • In Python, the deep copy method will change to the original dictionary and it will not affect on deep-copied dictionary. To make a recursive process while copying the dictionary we can use the deep copy method. In this method, it passes a dictionary as an argument and returns a new dictionary with copy all the elements recursively.
  • While in the case of the deep copy() method when we create a new object, and it recursively inserts the copies of nested objects available in the dictionary.

Syntax:

Here is the Syntax of shallow copy

dict.copy()

Note: This method does not take any argument.

Example:

Let’s take an example and check how to copy elements from the original dictionary to a new dictionary by using the shallow copy() method

my_dictionary={'Micheal':14,'George':18,'John':[21,34,56]}

new_you_dict=my_dictionary.copy()
print("copy dictionary",new_you_dict)
new_you_dict['John'].append(10)
print("updated dictionary",my_dictionary)

In the above code first, we initialize a dictionary ‘my_dictionary’ and now we will make a copy of it by using the dict.copy() method. After that use append() method to update the dictionary and display the result by using the print statement.

Here is the implementation of the following given code

Python dictionary shallow copy vs deep copy
Python dictionary shallow copy vs deep copy

Read Python Dictionary to CSV

By using deep copy() method

In Python, the deep copy method is used to create a deep copy of any object. This method creates an independent copy of the new objects and all nested objects.

Syntax:

Here is the Syntax of the deep copy() method

copy.deepcopy()

Note: In Python, the deep copy() method passes dict as an argument.

Source Code:

import copy
my_dictionary={'Potter':26,'James':49,'oliva':[15,27,119]}

new_dict=copy.deepcopy(my_dictionary)
new_dict['oliva'].append(91)
print(new_dict)

In the above program, we use the deepcopy() method to declare a similar dictionary from the original dictionary. To do this task first we will import a copy module and then initialize a dictionary.

After that use deepcopy() method and pass ‘my_dictionary’ as an argument and return a new dictionary with copy all objects.

Here is the Screenshot of the following given code

Python dictionary shallow copy vs deep copy
Python dictionary shallow copy vs deep copy

Read: Python find max value in a dictionary

Python dictionary copy deep

  • Let us see how to deep-copy copy a dictionary in Python.
  • By using the copy.deepcopy() method we can copy an element from the original dictionary and store them into a new object. To achieve the deep copying method we can import the ‘copy’ module which contains a deep copy operation. This method returns a new dictionary with copied values of the passed dictionary.
  • In Python, the deepcopy method builds a new object and then, recursively, insert copies into it the objects and will not affect the deep-copied dictionary.

Syntax:

Here is the Syntax of deep-copy() method

copy.deepcopy(dict)

Example:

Let’s take an example and check how to copy elements from the original dictionary by using the deepcopy() method.

Source Code:

import copy

my_new_dict = {"Germany":47, "Europe":93, 'Spain':{"France":83}}
cpy_ditionary = copy.deepcopy(my_new_dict)
print(cpy_ditionary)

my_new_dict["Spain"]["France"] = 71
print(my_new_dict)

In the above program first, we declare a dictionary ‘my_new_dict’ and then use the copy.deepcopy() method to copy ‘my_new_dict’ elements in the new ‘cpy_dictionary’. After that, we modify the new copy and put some changes in the original dictionary.

Note: As you can see from the output any change in ‘cpy_dictionary’ is not reflected in ‘my_new_dictionary’.

Here is the execution of the following given code

Python dictionary copy deep
Python dictionary copy deep

Read: Get all values from a dictionary Python

Python dictionary copy method

  • In a Python dictionary, the copy() is a built-in function that always returns a copy of a given dictionary. This method returns a shallow copy of the dictionary and does take any argument.
  • This method will help the user to copy every single element from the dictionary and contains them into a shallow dictionary and it does not update the original dictionary just returns a copy of the dictionary.

Syntax:

Here is the syntax of the dictionary copy() method

dictionary.copy()

Example:

See the following example

Stu_dict = {'stu_name': 'Bill', 'stu_gender': 'm'}

new_dict = Stu_dict.copy()
print("copy dictionary",new_dict)

Here is the execution of the following given code

Python dictionary copy method
Python dictionary copy method

Read: Python dictionary of tuples

Python dictionary copy by value

  • Let us see how to copy a dictionary by value in Python.
  • In this example to copy dictionary by values, we should find a way to create a new object in the memory. To perform this particular task we can use the concept of passed as an input to the function and it will help to create a new instance of the object.

Source Code:

def new_dict(m):
    m['George']=16
m={}
new_dict(m)
print("Copy dictionary",m)

In the above example first, we define a function ‘new_dict’ and then pass ‘m’ as an argument where m is an empty dictionary. Now pass a value as an input to the function and print the result.

Here is the execution of the following given code

Python dictionary copy by value
Python dictionary copy by value

Read: Python creates a dictionary from two lists

Python dictionary copy keys

  • Here we can see how to copy a keys from dictionary in Python.
  • To solve this problem we can use the concept of dict.keys() and set method. The dict.keys() method returns a view object that displays a list of all keys which is available in the dictionary and the set method is used to create all the items. In this example, we can pass dict.keys() method as an argument.

Example:

my_dict = {'m': 25, 'x': 89, 'p': 14, 'l': 20} 

b = set(my_dict.keys())
print("Dictionary copy keys:",b)

Here is the Output of the following given code

Python dictionary copy keys
Python dictionary copy keys

How to copy a keys from dictionary – another approach

Source Code:

country_name = {"Brazil" : 28, "europe" : 167, "England" : 18}

new_dict = country_name.copy()
new_dict["europe"] =678
print("original dictionary: ", country_name)
print("Modify dictionary = ", new_dict)

Output:

Python dictionary copy keys
Python dictionary copy keys

Read: Get all values from a dictionary Python

Python dictionary copy and update

  • Let us see how to copy and update elements from the Python dictionary.
  • By using dict.copy() method we can copies the key-value in a original dictionary to another new dictionary and it will return a shallow copy of the given dictionary and it also helps the user to copy each and every element from the original dictionary.

Example:

val_dict={ 23:'k', 178:[7,1,5], 183: 'z'}

new_dictionary=val_dict.copy() 
print("Copy dict:",new_dictionary)

new_dictionary[23]=89
new_dictionary[178][1]='91' 
 
print("Copy dictionary updated:",new_dictionary)
print("Original Dictionary:",val_dict)

In the above programm first, we initialize a dictionary ‘val_dict’ with some key-value pair elements and then use the copy() method on it to declare a shallow copy. After that update the item list and change in the given dictionary.

Here is the implementation of the following given code

Python dictionary copy and update
Python dictionary copy and update

Read: Python dictionary comprehension

Python dictionary Copy to list

  • Here we can see how to convert a copied dictionary into a list in Python.
  • In Python dictionary, the copy() method means a new collection object which stores the reference of the original values and it declares a new instance, and the elements are copied into the new instance.

Source Code:

student_information = {'Elite':127, 'Micheal':169, 'James':81, 'adam':43, 'joseph':189 }

cpy_dict = student_information.copy()
print("New dictionary: ",cpy_dict)
b = cpy_dict.items()
print("New list",b)

In the above program, first, we will initialize a dictionary and then use a copy() method on it and display the result. After that, we are going to use dict.items() method this function will help the user to convert the copied dictionary into a list.

Here is the Screenshot of the following given code

Python dictionary Copy to list
Python dictionary Copy to list

Read: Python dictionary find a key by value

Python Copy dictionary without reference

  • Here we can see how to copy the dictionary without taking any reference value in Python.
  • To perform this task we can apply the concept of the copy() method but in this example, we did not pass any variable as an argument in the copy() method. we simply create two variables ‘new_dict’, ‘org_dict’, and store them into a ‘c’ variable.

Example:

org_dict = {5: [9,1,6]}

new_dict = org_dict.copy()
c= org_dict, new_dict
print(c)
org_dict[5].append(34)
d=org_dict, new_dict
print(d)

Here is the execution of the following given code

Python Copy dictionary without reference
Python Copy dictionary without reference

Read: Python dictionary pop

Python copy dictionary to new variable

  • Let us see how to copy a dictionary and store them into a new variable in Python.
  • By using the deepcopy() method we can solve this task. In Python, the deep copy method will update to the original dictionary and it will not affect on deep-copied dictionary.

Source Code:

import copy

l = {'o':19}
g = copy.deepcopy(l)
print(g)

In the above code first, we will import a copy module then create a dictionary ‘l’ with some key-value pair element. After that use deepcopy() method and pass dictionary variable ‘l’ as an argument.

Output:

Python copy dictionary to new variable
Python copy dictionary to a new variable

Related Posts:

In this Python tutorial, we have discussed the Python dictionary Copy. Here we will also cover the following topics:

  • Python dictionary copy vs deep copy
  • Python dictionary copy deep
  • Python dictionary copy method
  • Python dictionary copy by value
  • Python dictionary copy keys
  • Python dictionary copy and update
  • Python dictionary copy to list
  • Python copy dictionary without reference
  • Python copy dictionary to new variable