28.10.2021 Views

Python Tutorial ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

7

8

9

10

dict.keys()

Returns list of dictionary dict's keys

dict.setdefault(key, default=None)

Similar to get(), but will set dict[key]=default if key is not already in dict

dict.update(dict2)

Adds dictionary dict2's key-values pairs to dict

dict.values()

Returns list of dictionary dict's values

The methods are explained below individually:

dict.clear()

Description

The method clear() removes all items from the dictionary.

Syntax

Following is the syntax for clear() method:

dict.clear()

Parameters

• NA

Return Value

This method does not return any value.

Example

The following example shows the usage of clear() method.

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7};

print "Start Len : %d" % len(dict)

dict.clear()

print "End Len : %d" % len(dict)

Let us compile and run the above program, this will produce the following result:

Start Len : 2

End Len : 0

dict.copy()

Description

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

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!