Computer Science
What is the use of copy( ) function ?
Python Dictionaries
3 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
1 Like
Related Questions
Can you use sum( ) for calculating the sum of the values of a dictionary ?
What do you understand by shallow copy of a dictionary ?
Discuss the working of copy( ) if
(i) the values are of immutable types,
(ii) the values are of mutable types.
Which of the following will result in an error for a given valid dictionary D?
- D + 3
- D * 3
- D + {3 : "3"}
- D.update( {3 : "3"})
- D.update { {"3" : 3}}
- D.update("3" : 3)