Python Dictionary copy() Method

Copies all the items in the dictionary to another dictionary which is then returned. Changes to the old dictionary do not the copied dictionary, and changes in the copied dictionary do not affect the old dictionary.

Syntax

Python
dictionary.copy()

Example

Python
oldProducts = {
    'toothbrush': 2.50, 
    'tomato soup': 1.99
}

newProducts = oldProducts.copy()

oldProducts['toothbrush'] = 1.75 # change the old product price
newProducts['toothpaste'] = 3.95 # add a new product

print(f'Old products: {oldProducts}')
print(f'New products: {newProducts}')

Output

Old products: {'toothbrush': 1.75, 'tomato soup': 1.99}
New products: {'toothbrush': 2.5, 'tomato soup': 1.99, 'toothpaste': 3.95}