×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Dictionary copy() Method (with Examples)

By IncludeHelp Last updated : December 21, 2024

In this tutorial, we will learn about the copy() method of a dictionary with its usage, syntax, parameters, return type, and examples.

Python Dictionary copy() Method

The copy() is an inbuilt method of dict class that is used to create a copy of the dictionary. The method is called with this dictionary and returns a shallow copy of this dictionary.

Syntax

The following is the syntax of copy() method:

dictionary_name.copy()

Parameter(s):

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'dict'>, it returns the dictionary.

Example 1: Use of Dictionary copy() Method

# dictionary declaration
student = {
  "roll_no": 101,
  "name": "Shivang",
  "course": "B.Tech",
  "per" : 98.5
}

# printing dictionary
print("data of student dictionary...")
print(student)

# copying dictionary
student_x = student.copy()

# printing dictionary
print("data of student_x dictionary...")
print(student_x)

Output

data of student dictionary...
{'roll_no': 101, 'course': 'B.Tech', 'name': 'Shivang', 'per': 98.5}
data of student_x dictionary...
{'roll_no': 101, 'course': 'B.Tech', 'name': 'Shivang', 'per': 98.5}

Example 2: Use of Dictionary copy() Method

# dictionary declaration
data = {"a": 1, "c": 3, "b": 5, "d": 4}

# printing dictionary
print("Original Dictionary...")
print(data)

# copying dictionary
result = data.copy()

# printing copied dictionary
print("Copied Dictionary...")
print(result)

Output

Original Dictionary...
{'a': 1, 'c': 3, 'b': 5, 'd': 4}
Copied Dictionary...
{'a': 1, 'c': 3, 'b': 5, 'd': 4}


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.