Ways to copy dictionary in python


Dictionary in python is a collection data type that stores information in the form of keys which have their corresponding values. It is unordered in nature and the stored data can be manipulated i.e.; it is changeable. We use dictionary to perform various operations, its application extends in the field of data base management, machine learning and web framework development.

In this article we will perform a basic dictionary-based operation explaining the different ways in which we can copy a dictionary element from an already existing dictionary. Before we dive deep into the topic, let’s quickly go through the overview of this article.

What is a dictionary?

A dictionary in python is a collection data type used to store data. Values are assigned to different keys. Keys are immutable i.e., for every they cannot be changed. Each key can contain different values but a single value cannot be associated with more than one key. For python dictionaries are objects with the data type “dict”

Creating a dictionary

A dictionary can be created with the help of curly braces. The syntax for this is −

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]} 

Here, “Name” is a key with three values and similarly “Age” is also a key with three values. These values can be of any data type. On the other hand, keys can also be of different data types but the condition is that it should be immutable. Eg: -strings, tuples, integers.

Now that we know the process of dictionary creation and various properties associated with it, we will understand the operation of copying a dictionary.

What does copying a dictionary mean?

When we say we will copy a dictionary it means we will copy the key value pairs from a dictionary source to our local dictionary. There are multiple methods that can be used to complete this operation −

Using the copy() method

This method creates a replica of the original dictionary. One noticeable detail about this method is that when we make changes to the copied dictionary, it does not reflect in the original dictionary but when the original dictionary is altered, we would observe changes in the copied version as well. Let’s see its implementation.

Example

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = dict1.copy()
print (dict2)

Output

{'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]} 

Now let’s see what changes are reflected when we manipulate the copied dictionary −

Example

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = dict1.copy()
dict2["Name"] = ["ARJUN", "VIJAY", "RAVI"]
print("The source dictionary is", dict1)
print("The copied dictionary is", dict2)

Output

The source dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}
The copied dictionary is {'Name': ['ARJUN', 'VIJAY', 'RAVI'], 'Age': [18, 22, 25]}

As we can see, no changes are reflected in the source dictionary because of the shallow copy creation. The copied dictionary is referring to the source dictionary.

Using dictionary comprehension

This method uses dictionary comprehension to iterate and add the elements from the source dictionary to the new dictionary.

We will traverse through the source dictionary and use items() method to add the key value pairs in the new dictionary. Let’s see its implementation −

Example

Following is an example for this. Here,

  • We created a source dictionary.

  • We used dictionary comprehension to traverse through the source dictionary and add key value pair with the help of items().

  • We manipulated the copied dictionary and printed both the versions.

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = {keys: values for keys, values in dict1.items()}
print("The source dictionary is", dict1)
dict2["Age"] = [33, 23, 21]
print("The copied dictionary is", dict2)

Output

The source dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}
The copied dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [33, 23, 21]}

Using the dict() method

In this method, we will create a new dictionary with the help of dict() method. In the parameter we will pass the source dictionary. The passed dictionary will be automatically copied. Let’s see its implementation.

Example

Following example copies the contents of a dictionary using the dict() method. Here,

  • We changed the value- “ROHIT” of the key- “Name” to “MAHI”.

  • After copying, we printed both the dictionaries.

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = dict(dict1)
print("The source dictionary is", dict1)
dict2["Name"] = ["MAHI", "AJAY", "RAGHAV"]
print("The copied dictionary is", dict2)

Output

The source dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}
The copied dictionary is {'Name': ['MAHI', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}

Conclusion

In this article we discussed the various methods involved in copying a dictionary from a source. We understood the concept of shallow copy and observed the behaviour of key value pairs.

Updated on: 27-Feb-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements