Dictionaries in Python are a collection of key-value pairs of data. These are used in many manipulations in Python and are one of the most important data structures in python. To calculate the length of the dictionary, the built-in
len() method is used.
len() method in Python:
This in-built method takes a Python object as an argument and returns the number of items in it. It can be used on strings, lists, dictionaries, tuples, and sets. The only thing to note down is the argument must be either a sequence or a collection.
Finding length of a simple dictionary:
Consider the following details of a person :
Name:Steve Age:30 Designation:Programmer
This information can be denoted by a simple dictionary. A simple dictionary contains just some key-value pairs. They are not much complex. When the
len() method is applied, it directly gives the answer. It gives the number of entries in the dictionary. For example:
len() method : 3 len() method with keys() : 3 len() method with values(): 3
Finding length of nested dictionary:
Consider the following details about a person:
Name:Steve Age:30 Designation:Programmer address: Street:Brigade Road City:Bangalore Country:India
This kind of information can be represented using nested dictionaries. The nested dictionaries contain dictionaries within dictionaries. In a key-value pair, for a key, another dictionary can be the value. In such cases, if the dictionary is simply passed to the
len() method, the output will not be correct. Look at the code below:
len() method : 4 len() method with keys() : 4 len() method with values(): 4
Here whichever method you apply, You will get only ‘4’ as the output. But the actual number of entries is ‘7’. The keys are name, age, designation, address, street, city, and country. The method considers the inner dictionary which is a value for one of the keys as a single value. To overcome this problem, we need to explicitly add the length of the inner dictionary to the outer one. It can be coded as given below:
The length of the nested dictionary is 7
Now it works fine!!!
However, is it possible to explicitly program to add the length of inner dictionaries every time? What if we do not know in prior how many inner dictionaries are there? Now consider the following detail:
Name: first name:Steve last name:Jobs Age:30 Designation:Programmer address: Street:Brigade Road City:Bangalore Country:India
Here we have two inner dictionaries. It is not an optimized way to explicitly add the length of the inner dictionaries every time. We can solve this problem by combining
This method takes two arguments. The first one is object. We need to pass a Python object to it. The object may be an int, a float, an iterable, or anything. The second argument to be passed is type/class. It denotes the type/class against which the first argument is checked for its instance. This method can help us find the length of a nested dictionary.
The idea is to first store the length of the whole dictionary in a variable (for say ‘length’ here). Then iterate through all the
values()of the dictionary and check whether it is an instance of
'True' then the length of that inner dictionary is found and added to the variable
length. In this way the total length of a nested dictionary could be found. The sample code is given below.
The length of the dictionary is 9
If the dictionary is further deeply nested like below:
Name: first name:Steve last name:Jobs Age:30 Designation:Programmer address: Street: St_number:4 St_name:Brigade Road City:Bangalore Country:India
Then modify the code as given below:
The total length of the dictionary is 11
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.