Calculating the length of the dictionary
Now, we are going to learn about how we can get length of dictionary python. By length of the dictionary, we mean to find how many(elements/items) key-value pairs are there in the dictionary. For getting the length of the dictionary, we are going to use the len function. Using the len function, we can also get the length of some other objects as well, like the list, string, tuple, etc…
Get Length Of Dictionary Python
Have a look at the below program, where we have a dictionary, and we are trying to find the length (number of elements) of the dictionary. You can count them, they are 5 in number, but let’s do this with help of a program, using the len function.
As you can see in the above program, we are having a simple dictionary, and then we are trying to print the length of the dictionary. To get the length of the dictionary, we are making use of the len function.
As you can also count, the number of elements in the dictionary is 5. the len function returns the number of key-value pairs in the dictionary. You can also store the value somewhere, to use it in the program, or you can also call the len function in the program as and when required.
If you have a look at the output, the output comes out to be 5.