Dictionaries are Python’s implementation of an associative array data structure. A dictionary is a grouping of key-value pairs. Each key pair is represented by a key pair and its associated value.
A dictionary is defined by a list of key value pairs enclosed in curly braces and separated by a comma. The value of each key is separated by the column ‘:’.
It is not possible to sort a dictionary solely to obtain a representation of the sorted dictionary. Dictionary entries are ordered by default, but other data types, such as lists and tuples, are not. As a result, you’ll need an ordered data form like a list—most likely a list of tuples.
Examples:
Input:
dictionary = {'this': 200, 'is':100, 'BTechGeeks':300}
Output:
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300} New dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}
Copy a Dictionary:
There are several ways to copy a dictionary some of them are:
- Using loop and copying element by element
- Using = operator
- Using copy() to create a shallow copy of dictionary
- Using copy.deepcopy() to copy a dictionary
Method #1:Using loop and copying element by element
This technique involves traversing the entire dictionary and copying each and every element pointed by the key to a previously declared new dictionary.
Approach:
- Take a empty new dictionary.
- Traverse the old dictionary using for loop.
- Copy the key and value of old dictionary element by element.
Below is the implementation:
# given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # Take a empty dictionary newdict = {} # Traverse the old dictionary for key in dictionary: newdict[key] = dictionary[key] # print old dictionary print("Old dictionary : ", dictionary) # print new dictionary print("New dictionary : ", newdict)
Output:
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300} New dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}
Method #2: Using = operator
We can simply copy a dictionary using = operator.
Below is the implementation:
# given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # copying dictionary to new dictionary newdict = dictionary # print old dictionary print("Old dictionary : ", dictionary) # print new dictionary print("New dictionary : ", newdict)
Output:
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300} New dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}
Explanation:
- We begin by creating a dictionary dictionary. And then use the code line newlist=dictionary to copy it to a new object dict2.
- This procedure copies each object’s references from dictionary to the new dictionary newdict.
- As a consequence, changing any aspect of newdict will change dictionary and vice versa.
The above code shows that when we update any(iterable or non-iterable) object in newdict, the same change is seen in dictionary.
Method #3: Using copy() to create a shallow copy of dictionary
A shallow copy is when a new dictionary object is formed and references to existing dictionary objects are inserted into it.
In Python, the dictionary copy() method creates a shallow copy of the given dictionary. It’s identical to what we saw before when copying elements by traversing through a dictionary.
Below is the implementation:
# given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # copying dictionary to new dictionary using copy newdict = dictionary.copy() # updating new dictionary newdict['hello'] = 400 # print old dictionary print("Old dictionary : ", dictionary) # print new dictionary print("New dictionary : ", newdict)
Output:
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300} New dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300, 'hello': 400}
Explanation:
- We initialize the dictionary dictionary with any values. To make a shallow copy, use the copy() method on it.
- We update the new elements and see the corresponding shift in the original dictionary after the copy is made.
- Changes in non-iterable elements of newdict have no impact on the original dictionary, just as they do with the element-by-element copying technique.
- The change is reflected in the given dictionary, dictionary, for iterable ones like lists.
Method #4: Using copy.deepcopy()Â to copy a dictionary
In Python, the deepcopy() method is part of the copy module. It creates a new dictionary from the passed dictionary’s elements. This method recursively copies all of the elements of the given dictionary.
Below is the implementation:
# importing copy import copy # given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # copying dictionary to new dictionary using deepcopy newdict = copy.deepcopy(dictionary) # updating new dictionary newdict['hello'] = 400 # print old dictionary print("Old dictionary : ", dictionary) # print new dictionary print("New dictionary : ", newdict)
Output:
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300} New dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300, 'hello': 400}
Related Programs:
- python how to check if a key exists in dictionary
- how to convert a list to dictionary in python
- python how to sort a dictionary by key or value
- python how to get all keys with maximum value in a dictionary
- python how to create a list of all the keys in the dictionary
- python how to create a list of all the values in a dictionary
- how to code a scraping bot with selenium and python