When I started building data-driven applications in Python over a decade ago, I realized how often I needed to check the size of my datasets.
Whether I was processing financial records in New York or managing user profiles for a tech startup, knowing the exact count of my dictionary items was crucial.
In this tutorial, I will show you exactly how to get the length of a dictionary using my favorite firsthand methods.
Use the len() Function
The most common way I find the length of a dictionary is by using the built-in len() function. It is fast, efficient, and is the standard way most developers handle this task in their daily work.
In the example below, I’ll create a dictionary representing a small inventory for a tech store in San Francisco to show you how it works.
# Inventory of a tech store in San Francisco
tech_inventory = {
"MacBook Pro": 15,
"iPhone 15": 30,
"iPad Air": 20,
"Apple Watch": 25,
"AirPods Max": 10
}
# Getting the number of items (key-value pairs)
inventory_count = len(tech_inventory)
print(f"Total product categories in stock: {inventory_count}")In this code, len() counts the number of top-level keys in the tech_inventory dictionary. Since there are five keys (the product names), the function returns 5.
Get Length of a Nested Dictionary
Sometimes, I work with more complex data structures, such as a nested dictionary where values are also dictionaries.
When you use len() on a nested dictionary, it only counts the top-level keys, not the items inside the nested ones.
Here is how I handle a list of employees in a Seattle-based company:
# Employee data for a Seattle tech firm
company_staff = {
"Engineering": {
"Manager": "Alice",
"Lead": "Bob",
"Developer": "Charlie"
},
"Marketing": {
"Manager": "David",
"Lead": "Eve"
}
}
# Count top-level departments
department_count = len(company_staff)
# Count employees in the Engineering department
engineering_count = len(company_staff["Engineering"])
print(f"Number of departments: {department_count}")
print(f"Number of roles in Engineering: {engineering_count}")You can see the output in the screenshot below.

If you need to find the total number of all nested items, you would usually need to iterate through the dictionary or use a recursive function.
Check if a Dictionary is Empty
In many of my projects, I use the length of a dictionary to check if it contains any data before I process it.
If the length is 0, it means the dictionary is empty, which is a common scenario when fetching data from an API.
# Simulating an empty search result for a real estate app in Miami
property_search_results = {}
if len(property_search_results) == 0:
print("No properties found matching your criteria in Miami.")
else:
print(f"Found {len(property_search_results)} properties!")You can see the output in the screenshot below.

While you can also use if not property_search_results:, using len() is a very explicit way to show your intent in the code.
Get the Memory Size of a Dictionary
While len() tells you how many items are in a Python dictionary, it doesn’t tell you how much memory those items occupy.
When I’m optimizing high-performance applications, I use the sys module to find the size of the dictionary in bytes.
import sys
# Data for a car dealership in Los Angeles
car_data = {
"Brand": "Tesla",
"Model": "Model 3",
"Year": 2024,
"Battery": "Long Range",
"Autopilot": True
}
# Getting the size in bytes
size_in_bytes = sys.getsizeof(car_data)
print(f"The dictionary occupies {size_in_bytes} bytes in memory.")You can see the output in the screenshot below.

Keep in mind that sys.getsizeof() only returns the size of the dictionary object itself, not the objects it points to.
Get Length via Keys and Values
You can also find the length by specifically targeting the keys or the values using the .keys() and .values() methods.
I don’t use this method as often as the direct len(dict) call, but it can be useful in specific logic flows.
# Customer loyalty points for a cafe in Chicago
customer_points = {
"John": 120,
"Sarah": 450,
"Mike": 300
}
# Getting length from keys and values
keys_length = len(customer_points.keys())
values_length = len(customer_points.values())
print(f"Number of customers: {keys_length}")
print(f"Number of point records: {values_length}")Both will give you the same result as len(customer_points) because every key must have a value in a dictionary.
I hope you found this tutorial on finding the length of a dictionary in Python useful!
Whether you are just starting or have been coding for years, these methods are essential tools for your development toolkit.
You may read:
- Fix the IndexError: tuple index out of range Error in Python
- Create a Tuple from the List in Python
- Find the Length of a Tuple in Python
- Add Tuples to Lists in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.