Python dictionary copy() method

Python Dictionary copy() Method

Description

The Python dictionary method copy() returns a shallow copy of a dictionary.

Syntax

The syntax of the copy() method is as follows:

dict.copy()

Parameters

None

Return Value

This method returns a shallow copy of the dictionary.

Example

The following example demonstrates the use of the copy() method.

dict = {'Name': 'Zara', 'Age': 7};
print "Start Len : %d" % len(dict)
dict.clear()
print "End Len : %d" % len(dict)

When we run the above program, we will get the following output −

New Dictionary : {'Age': 7, 'Name': 'Zara'}

Leave a Reply

Your email address will not be published. Required fields are marked *