A dictionary contains the pair of the keys & values (represents key : value), a dictionary is created by providing the elements within the curly braces ({}), separated by the commas.
There are some of the ways to copy dictionaries,
- Using the copy() method
- Using the dict() method
1) Copy dictionaries using the copy() method
The copy() method is a built-in method in Python, which can be used to create a copy of the dictionary.
Syntax:
Program:
Output:
Dictionary 'dict_a' is... {'age': 21, 'id': 101, 'name': 'Amit'} Dictionary 'dict_b' is... {'age': 21, 'id': 101, 'name': 'Amit'}2) Copy dictionaries using the dict() method
The dict() method is a built-in method in Python, which is used to create a new dictionary and can also be used to create a copy of the dictionary by creating a new dictionary from an existing dictionary.
Syntax:
Program:
Output:
Dictionary 'dict_a' is... {'id': 101, 'name': 'Amit', 'age': 21} Dictionary 'dict_b' is... {'id': 101, 'name': 'Amit', 'age': 21}need an explanation for this answer? contact us directly to get an explanation for this answer