Python copy Dictionary is one of a Dictionary function, used to shallow copy the dictionary items (key value pairs). In this section, we discuss how to use this Python Dictionary copy function with practical examples.
The syntax of the Python Dictionary copy function is:
dictionary_name.copy()
Python Dictionary copy Example
The copy dictionary function copies the keys, values in a given dictionary to another dictionary. The below code copy items in the emp dictionary to newEmp and newEmp2.
TIP: Please refer to the Dictionary to understand Python Dictionaries.
# Python Dictionary copy Example emp = {'name': 'John', 'age': 25 , 'job': 'HR', 'Sal': 725000} print("Dictionary: ", emp) print("Dictionary Length: ", len(emp)) newEmp = emp.copy() print("\nDictionary: ", newEmp) print("Dictionary Length: ", len(newEmp)) newEmp2 = emp.copy() print("\nDictionary: ", newEmp2)
OUTPUT