Python Dictionary copy() Method (with Examples)

Python Dictionary copy() Method: In this tutorial, we will learn about the copy() method of a dictionary with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 12, 2023

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 ↻





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