Dictionary copy() Function in Python:
The copy() method makes a duplicate of the specified dictionary(copy).
Syntax:
dictionary.copy()
Parameters: This method doesn’t accept any parameters
Return Value:
This method gives you a shallow copy of the dictionary. It makes no changes to the original dictionary.
Examples:
Example1:
Input:
Given dictionary = {20: 'good', 30: 'morning', 40: 'btechgeeks'}
Output:
The given original dictionary is: {20: 'good', 30: 'morning', 40: 'btechgeeks'} The copied new dictionary is: {20: 'good', 30: 'morning', 40: 'btechgeeks'}
Example2:
Input:
Given dictionary = {'hello': 100, 'btechgeeks': 200}
Output:
The given original dictionary is: {'hello': 100, 'btechgeeks': 200} The copied new dictionary is: {'hello': 100, 'btechgeeks': 200}
Dictionary copy() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the dictionary as static input and store it in a variable.
- Copy the given dictionary into a new dictionary using the copy() function.
- Store it in another variable.
- Print the given original dictionary.
- Print the copied new dictionary.
- The Exit of the Program.
Below is the implementation:
# Give the dictionary as static input and store it in a variable. gvn_dict = {20: 'good', 30: 'morning', 40: 'btechgeeks'} # Copy the given dictionary into a new dictionary using the copy() function. # Store it in another variable. new_dict = gvn_dict.copy() # Print the given original dictionary. print("The given original dictionary is:") print(gvn_dict) # Print the copied new dictionary. print("The copied new dictionary is:") print(new_dict)
Output:
The given original dictionary is: {20: 'good', 30: 'morning', 40: 'btechgeeks'} The copied new dictionary is: {20: 'good', 30: 'morning', 40: 'btechgeeks'}
Difference between Dictionary copy() Function , = Operator
When the copy() method is used, a new dictionary is created that contains a copy of the original dictionary’s references.
When the = operator is used, it creates a new reference to the original dictionary.
Approach:
- Give the dictionary as static input and store it in a variable.
- Copy the given dictionary into a new dictionary using the ‘=’ Operator.
- Store it in another variable.
- Remove all the elements from the new dictionary using the clear() function.
- Print the given original dictionary.
- Print the copied new dictionary.
- The Exit of the Program.
# Give the dictionary as static input and store it in a variable. gvn_dict = {'hello': 100, 'btechgeeks': 200} # Copy the given dictionary into a new dictionary using the '=' Operator. # Store it in another variable. new_dict = gvn_dict # Remove all the elements from the new dictionary using the clear() function. new_dict.clear() # Print the given original dictionary. print("The given original dictionary is:") print(gvn_dict) # Print the copied new dictionary. print("The copied new dictionary is:") print(new_dict)
Output:
The given original dictionary is: {} The copied new dictionary is: {}
Method #2: Using Built-in Functions (User Input)
Approach:
- Take a dictionary and initialize it with an empty dictionary using dict() or {}.
- Give the number of keys as user input using int(input()) and store it in a variable.
- Loop till the given number of keys using for loop.
- Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
- Initialize the key with the value of the dictionary.
- Copy the given dictionary into a new dictionary using the copy() function.
- Store it in another variable.
- Print the given original dictionary.
- Print the copied new dictionary.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it with an empty dictionary using dict() or {}. gvn_dict = {} # Give the number of keys as user input using int(input()) and store it in a variable. numb_of_kys = int( input('Enter some random number of keys of the dictionary = ')) # Loop till the given number of keys using for loop. for p in range(numb_of_kys): # Inside the for loop scan the key and value as # user input using input(),split() functions # and store them in two separate variables. keyy, valuee = input( 'Enter key and value separated by spaces = ').split() # Initialize the key with the value of the dictionary. gvn_dict[keyy] = valuee # Copy the given dictionary into a new dictionary using the copy() function. # Store it in another variable. new_dict = gvn_dict.copy() # Print the given original dictionary. print("The given original dictionary is:") print(gvn_dict) # Print the copied new dictionary. print("The copied new dictionary is:") print(new_dict)
Output:
Enter some random number of keys of the dictionary = 2 Enter key and value separated by spaces = 100 hello Enter key and value separated by spaces = 200 btechgeeks The given original dictionary is: {'100': 'hello', '200': 'btechgeeks'} The copied new dictionary is: {'100': 'hello', '200': 'btechgeeks'}