Methods and Functions

Python Dictionary copy()

You often alter mutable collections like dictionaries while programming. For this, you will sometimes need to copy dictionaries in Python to make changes in the copy without changing the original. In Python, we use the copy() method to create a copy of a dictionary.

copy dictionaries in Python

copy() Parameters

Since the method creates a copy of the whole dictionary, it does not accept any parameters. It follows the syntax:

Return value from copy()

When we copy dictionaries in Python, using the copy() method, it returns a shallow copy of the original dictionary. In other words, it uses the original as a reference to create a new dictionary and does not make any changes in the original elements.

Example of how copy works for dictionaries?

Let us understand how to use the copy() for dictionaries with an example:

                    

# create dictionary
marks = { 'Science': 87, 'Social': 79, 'Math': 76}  #original
new = marks.copy()

print ('New =', new)
print ('Original =', marks)

Output:

                    

New = {'Science': 87, 'Social': 79, 'Math': 76}
Original = {'Science': 87, 'Social': 79, 'Math': 76}

The difference when using copy() method and = Operator to copy dictionaries

When we use the copy() method, Python creates a completely new dictionary object to store a copy of the reference values from the original dictionary.

However, the = operator only creates a new reference that remains linked to the original dictionary. If you make any changes in the copy, it will reflect in the original as well.

Example of using = operator to copy dictionaries

                    

# create list
fruits = {'apple': 5, 'banana': 8, 'grapes': 4}
new = fruits   # copy using = operator

# removing all the items in the dictionary
new.clear()  	

# items removed from both new and original

print ('New dictionary:', new)
print ('Original dictionary:', fruits)

Output:

                    

New dictionary:  {}
Original dictionary:  {}

Example of using copy() to copy dictionaries

                    

# create list
fruits = {'apple': 5, 'banana': 8, 'grapes': 4}
new = fruits.copy()  

# removing all the items in the dictionary
new.clear()  	

# items removed only from new

print ('New dictionary:', new)
print ('Original dictionary:', fruits)

Output:

                    

New dictionary: {}
Original dictionary: {'apple': 5, 'banana': 8, 'grapes': 4}

Questions and Answers

Q1. What does copy() do in Python?

The copy() method is a built-in function that lets you copy dictionaries in Python. Programmers often use it when they want to make changes only in the copied items and not in the original dictionary items.

Q2. Can you index a dictionary in Python?

Unlike sequences such as lists, tuples, etc., the items in Python dictionaries are not indexed by numbers (0,1,2,3, so on). Python offers a key: value indexing facility that is unique to dictionary elements. Each item in a dictionary is identified using a key name (or key) that has an associated value.

                    

# dictionary
my_dict = {'name': 'Phil', 'age': 50, 'country': 'America'}

In the above example, ‘name’ is a key, and ‘Phil’ is the value associated with it.

Q3. Can you slice a dictionary in Python?

No, you cannot slice dictionaries as they do not have index numbers. You can only slice sequences like lists using index numbers.

                    

word = 'Python program'

# slice Python
sliced_word = slice(6)

print(text[sliced_word])

# output: Python

Q4. Can dictionaries have duplicate values in Python?

Although Python does not support duplicate keys, the values associated with these keys can duplicate.

For example:

                    

# marks dictionary
marks = { 'Science': 87, 'Social': 79, 'Math': 79, 'English': 87}

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.