Definition:
This method of dictionary simply copies the entire dictionary into another variable, also the original dictionary remains unchanged.
Syntax:
Variable = Dictionary.copy()
This method takes no argument.
Working:
You can copy code here.
dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Age’: 23,
‘Male’: True,
‘Subject’: ‘Computers’}
x = dictionary.copy()
print(x)
Code:
- A dictionary with some key values is being taken.
- All the values of the dictionary are copied into variable ‘x’.
- Variable ‘x’ is being printed.
Output:
- Entire value of the dictionary is copied into x.
Interview questions:
- Can you copy certain items from the dictionary?
No, we cannot copy certain items from the dictionary as we cannot slice dictionaries, instead we can copy entire items using built-in copy() method.
Related Post.