Python Dictionary copy() method returns a shallow copy of the dictionary. let's see the Python Dictionary copy() method with examples:
Examples
Input: original = {1:'geeks', 2:'for'}
new = original.copy() // Operation
Output: original: {1: 'one', 2: 'two'}
new: {1: 'one', 2: 'two'}
Syntax of copy() method
Syntax: dict.copy()
Return: This method doesn't modify the original, dictionary just returns copy of the dictionary.
Python Dictionary copy() Error:
As we are not passing any parameters, there is no chance of any error.
Example 1: Examples of Python Dictionary copy()
Python program to demonstrate the working of dictionary copy.
Python3
original = {1: 'geeks', 2: 'for'}
# copying using copy() function
new = original.copy()
# removing all elements from the list
# Only new list becomes empty as copy()
# does shallow copy.
new.clear()
print('new: ', new)
print('original: ', original)
Output:
new: {}
original: {1: 'geeks', 2: 'for'}
Example 2: Python Dictionary copy() and update
Python program to demonstrate the working of dictionary copy. i.e. Updating dict2 elements and checking the change in dict1.
Python3
# given dictionary
dict1 = {10: 'a', 20: [1, 2, 3], 30: 'c'}
print("Given Dictionary:", dict1)
# new dictionary and
# copying using copy() method
dict2 = dict1.copy()
print("New copy:", dict2)
# Updating dict2 elements and
# checking the change in dict1
dict2[10] = 10
dict2[20][2] = '45' # list item updated
print("Updated copy:", dict2)
Output:
Given Dictionary: {10: 'a', 20: [1, 2, 3], 30: 'c'}
New copy: {10: 'a', 20: [1, 2, 3], 30: 'c'}
Updated copy: {10: 10, 20: [1, 2, '45'], 30: 'c'}
Difference between shallow copy and deep copy
It means that any changes made to a copy of the object do not reflect in the original object. In python, this is implemented using “deepcopy()” function. whereas in shallow copy any changes made to a copy of an object do reflect in the original object. In python, this is implemented using the “copy()” function.
Example 1: Using copy()
Unlike copy(), the assignment operator does deep copy.
Python3
# Python program to demonstrate difference
# between = and copy()
original = {1: 'geeks', 2: 'for'}
# copying using copy() function
new = original.copy()
# removing all elements from new list
# and printing both
new.clear()
print('new: ', new)
print('original: ', original)
original = {1: 'one', 2: 'two'}
# copying using =
new = original
# removing all elements from new list
# and printing both
new.clear()
print('new: ', new)
print('original: ', original)
Output:
new: {}
original: {1: 'geeks', 2: 'for'}
new: {}
original: {}
Example 2: Using copy.deepcopy
Unlike deepcopy(), the assignment operator does deep copy.
Python3
import copy
# Python program to demonstrate difference
# between = and copy()
original = {1: 'geeks', 2: 'for'}
# copying using copy() function
new = copy.deepcopy(original)
# removing all elements from new list
# and printing both
new.clear()
print('new: ', new)
print('original: ', original)
original = {1: 'one', 2: 'two'}
# copying using =
new = original
# removing all elements from new list
# and printing both
new.clear()
print('new: ', new)
print('original: ', original)
Output:
new: {}
original: {1: 'geeks', 2: 'for'}
new: {}
original: {}
Similar Reads
Python Dictionary Methods
Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu
5 min read
Python Dictionary clear()
clear() method in Python is used to remove all items (key-value pairs) from a dictionary. After calling this method, the dictionary will become empty and its length will be 0. This method is part of the built-in dictionary operations in Python.Example:Pythond = {1: "geeks", 2: "for"} # using clear()
2 min read
Python Dictionary copy()
Python Dictionary copy() method returns a shallow copy of the dictionary. let's see the Python Dictionary copy() method with examples: Â Examples Input: original = {1:'geeks', 2:'for'} new = original.copy() // Operation Output: original: {1: 'one', 2: 'two'} new: {1: 'one', 2: 'two'}Syntax of copy()
3 min read
Python Dictionary fromkeys() Method
Python dictionary fromkeys() function returns the dictionary with key mapped and specific value. It creates a new dictionary from the given sequence with the specific value. Python Dictionary fromkeys() Method Syntax: Syntax : fromkeys(seq, val) Parameters : seq : The sequence to be transformed into
3 min read
Python Dictionary get() Method
Python Dictionary get() Method returns the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).Python Dictionary get() Method Syntax:Syntax : Dict.get(key, Value)Parameters: key: The key name of the item you want to return
3 min read
Python Dictionary items() method
items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p
2 min read
Python Dictionary keys() method
keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example:Pythond = {'A': 'Geeks', 'B': 'For', 'C': 'Ge
2 min read
Python Dictionary pop() Method
The Python pop() method removes and returns the value of a specified key from a dictionary. If the key isn't found, you can provide a default value to return instead of raising an error. Example:Pythond = {'a': 1, 'b': 2, 'c': 3} v = d.pop('b') print(v) v = d.pop('d', 'Not Found') print(v) Output2 N
2 min read
Python Dictionary popitem() method
popitem() method in Python is used to remove and return the last key-value pair from the dictionary. It is often used when we want to remove a random element, particularly when the dictionary is not ordered. Example:Pythond = {1: '001', 2: '010', 3: '011'} # Using popitem() to remove and return the
2 min read
Python Dictionary setdefault() Method
Python Dictionary setdefault() returns the value of a key (if the key is in dictionary). Else, it inserts a key with the default value to the dictionary. Python Dictionary setdefault() Method Syntax: Syntax: dict.setdefault(key, default_value)Parameters: It takes two parameters: key - Key to be sear
2 min read