- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS.Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Questions & Answers
- Python - Online Quiz
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python dictionary copy() Method
The Python dictionary copy() method is used to return a shallow copy of the current dictionary. A shallow copy of an object is one whose properties are identical to those of the source object from which the copy was made, sharing the same references (pointing to the same underlying values). In short, a new dictionary is created in which the references from the original dictionary is filled with a copy.
We can also copy the dictionary using the = operator, which points to the same object as the original. So, if any changes is made to the copied dictionary, it is also reflected in the original dictionary. In short, a new reference is created to the original dictionary.
Syntax
Following is the syntax of the Python dictionary copy() method −
dict.copy()
Parameters
This method does not accept any parameter.
Return Value
This method returns a shallow copy of the current dictionary.
Example
The following example shows the usage of Python dictionary copy() method. Here we are creating a dictionary 'dict_1', then creating a copy of it.
dict1 = {'Name': 'Zara', 'Age': 7}; dict2 = dict1.copy() print ("New Dictionary : %s" % str(dict2))
When we run above program, it produces following result −
New Dictionary : {'Name': 'Zara', 'Age': 7}
Example
Now, we are updating the elements of the second dictionary.Thereafter, checking whether the changes are reflected in the first dictionary or not.
dictionary = {5: 'x', 15: 'y', 25: [7, 8, 9]} print("The given dictionary is: ", dictionary) # using copy() method to copy d2 = dictionary.copy() print("The new copied dictionary is: ", d2) # Updating the elements in second dictionary d2[15] = 2 # updating the items in the list d2[25][1] = '98' print("The updated dictionary is: ", d2)
Following is an output of the above code −
The given dictionary is: {5: 'x', 15: 'y', 25: [7, 8, 9]} The new copied dictionary is: {5: 'x', 15: 'y', 25: [7, 8, 9]} The updated dictionary is: {5: 'x', 15: 2, 25: [7, '98', 9]}
Example
In the code below we are creating a dictionary 'dict_1'. Then a shallow copy of the original dictionary is created. After that an element is added to the shallow copy. Thereafter, the result is retrieved showing that the element gets appended to the shallow copy of the dictionary while the original dictionary remains unchanged.
# Creating a dictionary dict_1 = {"1": "Lion", "2": "Tiger"} print("The first dictionary is: ", dict_1) # Create a shallow copy of the first dictionary SC = dict_1.copy() print("The shallow copy of the dictionary is: ", SC) # Append an element to the created shallow copy SC[3] = "Cheetah" print("The shallow copy after adding an element is: ", SC) # No changes in the first dictionary print("There is no changes in the first dictionary: ", dict_1)
Output of the above code is as follows −
The first dictionary is: {'1': 'Lion', '2': 'Tiger'} The shallow copy of the dictionary is: {'1': 'Lion', '2': 'Tiger'} The shallow copy after adding an element is: {'1': 'Lion', '2': 'Tiger', 3: 'Cheetah'} There is no changes in the first dictionary: {'1': 'Lion', '2': 'Tiger'}
To Continue Learning Please Login
Login with Google