Python Dictionary copy function

This Python function shallow copy the dictionary items (key value pairs) and its syntax is shown below.

dictionary_name.copy()

Python Dictionary copy function Example

This function copies the keys, and values in a given dictionary to another one. The below code copies items in the emp to newEmp and newEmp2.

TIP: Please refer to the Dictionary to understand them in Python.

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)
Python Dictionary copy Example