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 1

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.