How to count elements in a nested Python dictionary?


To count elements in a Nested Dictionary, we can use the Built-in function len(). With that, we can also use a function that recursively calls and calculates the elements in an arbitrary depth nested dictionary.

Count Elements in a Dictionary

Let us first create a Python Dictionary and count its values using the len() method. Here, we have included 4 key-value pairs in the Dictionary and displayed them. Product, Model, Units, and Available are keys of the Dictionary. Except the Units key, all are having String values −

Example

# Creating a Dictionary myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print(myprod) # Count print(len(myprod))

Output

{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
4

Now, we will create a Nested Dictionary and count the elements.

Count Elements in a Nested Dictionary

To count elements in a nested dictionary, we have used the sum() function and calculated the dictionary values one-by-one within that. At the end, the count is displayed −

Example

# Creating a Nested Dictionary myprod = { "Product1" : { "Name":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" }, "Product2" : { "Name":"SmartTV", "Model": "LG", "Units": 70, "Available": "Yes" }, "Product3" : { "Name":"Laptop", "Model": "EEK", "Units": 90, "Available": "Yes" } } # Displaying the Nested Dictionary print("Nested Dictionary = \n",myprod) print("Count of elements in the Nested Dictionary = ",sum(len(v) for v in myprod.values()))

Output

Nested Dictionary = 
 {'Product1': {'Name': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}, 'Product2': {'Name': 'SmartTV', 'Model': 'LG', 'Units': 70, 'Available': 'Yes'}, 'Product3': {'Name': 'Laptop', 'Model': 'EEK', 'Units': 90, 'Available': 'Yes'}}
Count of elements in the Nested Dictionary =  12

Count Elements in an Arbitrary Depth Nested Dictionary

To count elements in an arbitrary depth nested dictionary, we have created a custom Python function and used for loop within it. The loop recursively calls and calculate the depth of the dictionary. At the end, it returns the length −

Example

# Creating a Nested Dictionary myprod = { "Product" : { "Name":"Mobile", "Model": { 'ModelName': 'Nord', 'ModelColor': 'Silver', 'ModelProcessor': 'Snapdragon' }, "Units": 120, "Available": "Yes" } } # Function to calculate the Dictionary elements def count(prod, c=0): for mykey in prod: if isinstance(prod[mykey], dict): # calls repeatedly c = count(prod[mykey], c + 1) else: c += 1 return c # Displaying the Nested Dictionary print("Nested Dictionary = \n",myprod) # Display the count of elements in the Nested Dictionary print("Count of the Nested Dictionary = ",count(myprod))

Output

Nested Dictionary = 
 {'Product': {'Name': 'Mobile', 'Model': {'ModelName': 'Nord', 'ModelColor': 'Silver', 'ModelProcessor': 'Snapdragon'}, 'Units': 120, 'Available': 'Yes'}}
Count of the Nested Dictionary =  8

Updated on: 11-Aug-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements