Python Dictionary len() Method

Python 字典Python Dictionary


Description

The Python Dictionary len() function calculates the number of dictionary elements, i.e., the total number of keys.

Syntax

Syntax of the len() method:

len(dict)

Parameters

  • dict -- The dictionary whose number of elements is to be calculated.

Return Value

Returns the number of elements in the dictionary.

Example

The following example demonstrates how to use the len() function:

Example

#!/usr/bin/python

tinydict = {'Name': 'Example', 'Age': 7};
print "Length : %d" % len (tinydict)

The output of the above example is:

Length : 2

Python 字典Python Dictionary

Other Extensions