KnowledgeBoat Logo

Computer Science

What is the use of copy( ) function ?

Python Dictionaries

2 Likes

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, originald and newd are two isolated objects, but their contents still share the same reference i.e ('a','b')

Answered By

3 Likes


Related Questions