How to Use Dictionary copy() Method in PythonPython Basics


They copy() method gets a shallow copy of the dictionary.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

dict1 = {'Name': 'Gerryzhang', 'Age': 24,'Tel':123}
print(dict1)

dict2=dict1.copy()
print(dict2)

Output:

{'Name': 'Gerryzhang', 'Age': 24, 'Tel': 123}
{'Name': 'Gerryzhang', 'Age': 24, 'Tel': 123}

Syntax

dictionary.copy()

Parameters

The method doesn’t take any parameters.

Return Value

It returns a shallow copy of the dictionary.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us