Python Dictionary - len() function

The len() function when passed a Python dictionary object returns the number of elements present in the dictionary.

Dictionary Updated 10 Sep 2016 1 min read

Function Name

len(dict_in)

Parameters

dict_in - The dictionary for which the number of elements present to be counted.

Function overview

  • The len() function returns the number of elements stored in a Python dictionary object.
  • For an empty dictionary the len() function returns zero.

Parameters

dict_in _ a dictionary object for which number of items to be determined.

Example

python
# Example Python program that finds the total number

            # of items present in a Python dictionary

            d = {"a":1, "b":2, "c":3}

            elemCount = len(d)

            print("Number of elements:")

            print(elemCount)

Output

Output
Number of elements:

            3