Get length of dictionary in Python Last Updated : 10 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Python provides multiple methods to get the length, and we can apply these methods to both simple and nested dictionaries. Let’s explore the various methods.Using Len() FunctionTo calculate the length of a dictionary, we can use Python built-in len() method. It method returns the number of keys in dictionary. Python d ={'Name':'Steve', 'Age':30, 'Designation':'Programmer'} print(len(d)) Output3 Let's explore other methods of getting length of a dictionary in python:Table of ContentUsing a List ComprehensionUsing sum() with 1 for each itemGetting length of nested dictionaryUsing a Loop to Count Items (Manual Counting)Using len() to get length of Keys, Values or ItemsUsing List ComprehensionAnother less common method is to use a list comprehension, which can also be used to count the number of keys in a dictionary.Example: Python d = {"a": 1, "b": 2, "c": 3} length = len([key for key in d]) print(length) Output3 Explanation:This method creates a list of keys using a list comprehension ([key for key in my_dict]), and len() is then used to count the number of keys.Using sum() with 1 for each itemWe can also use the sum() function to count the number of items in a dictionary by adding 1 for each key-value pair.Example: Python d = {"a": 1, "b": 2, "c": 3} length = sum(1 for i in d) print(length) Output3 Getting length of nested dictionaryWhen working with nested dictionaries, we may want to count only the top-level keys or count the keys in nested dictionaries. If we want to get the length of the top-level dictionary, len() works just as it does with simple dictionaries. Python d = { "person1": {"name": "John", "age": 25}, "person2": {"name": "Alice", "age": 30}, "person3": {"name": "Bob", "age": 22} } length = len(d) print(length) Output3 Explanation:d contains 3 top-level key-value pairs, so len() returns 3.Using Loop to Count Items (Manual Counting)Another method to count items in a dictionary, including nested dictionaries, is using a loop. For nested dictionaries, we can manually iterate through all the nested keys and values. Python d = { "product1": {"name": "Laptop", "price": 800, "stock": 15}, "product2": {"name": "Smartphone", "price": 500, "stock": 30}, "product3": {"name": "Tablet", "price": 300, "stock": 25} } cnt = 0 # Loop through the top-level dictionary for key, val in d.items(): if isinstance(val, dict): # Check if the value is a nested dictionary # Loop through the nested dictionary for i in val: cnt += 1 # Count each key in the nested dictionary else: cnt += 1 # Count the top-level keys print(cnt) Output9 Explanation:We iterate over each key-value pair in the top-level dictionary d.If the value is a nested dictionary (as in the case of "person1", "person2", and "person3"), we loop through that nested dictionary and count its keys (name and age).We also count the top-level keys (person1, person2, person3).The total count of items (keys) in the entire dictionary is 6 (3 top-level keys + 3 keys inside the nested dictionaries).Using len() to get length of Keys, Values or ItemsWe can also use the len() function on the dictionary’s keys, values or items to determine the number of keys, values or key-value pairs. Python d = {"a": 1, "b": 2, "c": 3} # Number of Keys length = len(d.keys()) print(length) # Number of Values length = len(d.values()) print(length) # Number of Items length = len(d.items()) print(length) Output3 3 3 Comment More infoAdvertise with us Next Article Python - Value length dictionary E erakshaya485 Follow Improve Article Tags : Technical Scripter Python python-dict Python dictionary-programs Practice Tags : pythonpython-dict Similar Reads Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to 5 min read How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa 3 min read Python - Add Dictionary Items In Python, dictionaries are a built-in data structure that stores key-value pairs. Adding items to a dictionary is a common operation when you're working with dynamic data or building complex data structures. This article covers various methods for adding items to a dictionary in Python.Adding Items 3 min read Python Add Dictionary Key In Python, dictionaries are unordered collections of key-value pairs. Each item in a dictionary is accessed by its unique key, which allows for efficient storage and retrieval of data. While dictionaries are commonly used with existing keys, adding a new key is an essential operation when working wi 4 min read Python Access Dictionary In Python, dictionaries are powerful and flexible data structures used to store collections of data in key-value pairs. To get or "access" a value stored in a dictionary, we need to know the corresponding key. In this article, we will explore all the ways to access dictionary values, keys, and both 4 min read Python Change Dictionary Item A common task when working with dictionaries is updating or changing the values associated with specific keys. This article will explore various ways to change dictionary items in Python.1. Changing a Dictionary Value Using KeyIn Python, dictionaries allow us to modify the value of an existing key. 3 min read Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop 2 min read Get length of dictionary in Python Python provides multiple methods to get the length, and we can apply these methods to both simple and nested dictionaries. Letâs explore the various methods.Using Len() FunctionTo calculate the length of a dictionary, we can use Python built-in len() method. It method returns the number of keys in d 3 min read Python - Value length dictionary Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor 4 min read Python - Dictionary values String Length Summation Sometimes, while working with Python dictionaries we can have problem in which we need to perform the summation of all the string lengths which as present as dictionary values. This can have application in many domains such as web development and day-day programming. Lets discuss certain ways in whi 4 min read Like