CBSE Class 11 Computer Science Question 77 of 112

Dictionaries — Question 17

Back to all questions
17
Question

Question 17

What is the use of copy( ) function ?

Answer

The copy() function is used to create a shallow copy of a dictionary where only a copy of keys is created and the values referenced are shared by the two copies.
For example:

original_d = {1:'a', 2:'b'} 
new_d = original_d.copy()
print(new_d)
Output

{1: 'a', 2: 'b'}

Here, original_d and new_d are two isolated objects, but their contents still share the same reference i.e ('a','b')