Python: Create a shallow copy of a given dictionary
Write a Python program to create a shallow copy of a given dictionary. Use copy.copy
Sample Solution:
Python Code:
Sample Output:
Original dictionary: {'a': 1, 'b': 2, 'cc': {'c': 3}} Copy of the said list: {'a': 1, 'b': 2, 'cc': {'c': 3}} Change the value of an element of the original dictionary: {'a': 1, 'b': 2, 'cc': {'c': 10}} Second dictionary: {'a': 1, 'b': 2, 'cc': {'c': 10}} Original dictionary : {'x': 1, 'y': 2, 'zz': {'z': 3}} Copy of the said list: {'x': 1, 'y': 2, 'zz': {'z': 3}} Change the value of an element of the original dictionary: First dictionary: {'x': 1, 'y': 2, 'zz': {'z': 10}} Second dictionary (copy): {'x': 1, 'y': 2, 'zz': {'z': 10}}
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to create a deep copy of a given list.
Next: Write a Python program to create a deep copy of a given dictionary.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics