- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Ways to Copy Dictionary
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. They copy() method returns a shallow copy of the dictionary.
Example
#creating a dictionary original = {1:'vishesh', 2:'python'} # copying using copy() function new = original.copy() # removing all elements from the list Only new list becomes empty as #copy() does shallow copy. new.clear() print('new: ', new) print('original: ', original) # between = and copy() original = {1:'Vishesh', 2:'python'} # copying using copy() function new = original.copy() # removing all elements from new list # and printing both new.clear() print('new: ', new) print('original: ', original) original = {1:'one', 2:'two'} # copying using = new = original # removing all elements from new list # and printing both new.clear() print('new: ', new) print('original: ', original)
Output
('new: ', {}) ('original: ', {1: 'vishesh', 2: 'python'}) ('new: ', {}) ('original: ', {1: 'Vishesh', 2: 'python'}) ('new: ', {}) ('original: ', {})
- Related Articles
- Python - Ways to invert mapping of dictionary
- Python - Ways to create a dictionary of Lists
- Python - Ways to remove a key from dictionary
- Ways to copy a vector in C++
- Program to find number of ways to form a target string given a dictionary in Python
- How to convert Javascript dictionary to Python dictionary?
- Convert string dictionary to dictionary in Python
- How to define a Python dictionary within dictionary?
- Copy - Shallow and deep copy operations in Python
- Python - Convert flattened dictionary into nested dictionary
- Python Convert nested dictionary into flattened dictionary?
- How to create Python dictionary from the value of another dictionary?
- How to create nested Python dictionary?
- Add dictionary to tuple in Python
- Python – Concatenate Tuple to Dictionary Key

Advertisements