Skip to main content

copy()

The copy() function in Python is a method for dictionaries that returns a shallow copy of the dictionary. This means it creates a new dictionary with the same key-value pairs as the original dictionary. Any modifications made to the copied dictionary will not affect the original dictionary, but changes to nested mutable objects within the dictionary will be reflected in the copy. This function is useful for creating a duplicate of a dictionary without altering the original one.

Parameter Values

This function does not accept any parameters.

Return Values

The copy() method of a dictionary returns a new dict object.

How to Use copy() in Python

Example 1:

The copy() method in Python is used to make a shallow copy of a dictionary. It creates a new dictionary with the same key-value pairs as the original one.

original_dict = {'a': 1, 'b': 2}
copied_dict = original_dict.copy()
print(copied_dict)