Python Tutorial Python Advanced Python References Python Libraries

Python - Dictionary Length



The Python len() function can be used to find out the total number of key-value pairs in the dictionary. To add more to this, len() function returns the number of elements in an iterable. An iterable object can be any data structure like list, tuple, set, string and dictionary, etc.

Syntax

len(dict)

Parameters

dict Required. specify iterable like a dictionary to find out total number of key-value pairs in it.

Return Value

Returns the total number of key-value pairs in the dictionary.

Example:

In the example below, len() function is used to find out the total number of key-value pairs in the given dictionary.

Info = {
  'name': 'John',
  'age': 25,
  'city': 'London'
}

#finding total number of key-value
#pairs in the dictionary     
print(len(Info))

The output of the above code will be:

3

❮ Python Dictionary Methods

5