How to Get the Length of a Dictionary in Python?

2 weeks ago 16
Club.noww.in

Recently, someone asked me about the length of the Python dictionary. There are different methods to achieve this. In this tutorial, I will explain how to get the length of a dictionary in Python with examples.

To determine the length of a dictionary in Python, you can use the built-in len() function. This function takes the dictionary as an argument and returns the number of key-value pairs it contains. For example, if you have a dictionary state_population = {"California": 39538223, "Texas": 29145505}, calling len(state_population) will return 2, indicating that there are two key-value pairs in the dictionary.

Length of Dictionary in Python

A dictionary in Python is a collection of key-value pairs. Each key is unique, and it maps to a value. Dictionaries are mutable, meaning they can be changed after creation. They are incredibly useful for storing and organizing data.

To find the length of a dictionary, Python provides a built-in function called len(). This function returns the number of key-value pairs in the dictionary.

Syntax:

len(dictionary)

The len() function takes a dictionary as its argument and returns the number of items (key-value pairs) in the dictionary.

Let’s start with a simple example. Suppose we have a dictionary containing the population of various states in the USA.

state_population = { "California": 39538223, "Texas": 29145505, "Florida": 21538187, "New York": 20201249, "Pennsylvania": 13002700 } print(len(state_population)) # Output: 5

In this example, the len() function returns 5 because there are five key-value pairs in the state_population dictionary.

I executed the above Python code using VS code, and you can see the exact output:

length of dictionary python

Check out Convert a Dictionary to a List in Python

Find Length of Python Dictionary Values

Sometimes, we need to find the length of the values in a Python dictionary. This can be useful when the values are iterable objects like lists or strings.

Example:

Consider a dictionary where each key maps to a list of cities in that state.

state_cities = { "California": ["Los Angeles", "San Francisco", "San Diego"], "Texas": ["Houston", "Dallas", "Austin"], "Florida": ["Miami", "Orlando", "Tampa"] } city_lengths = {state: len(cities) for state, cities in state_cities.items()} print(city_lengths) # Output: {'California': 3, 'Texas': 3, 'Florida': 3}

In this example, we use a dictionary comprehension to create a new dictionary city_lengths where each key is the state, and the value is the number of cities in that state.

Here is the exact output in the screenshot below:

python dict length

Read Python Dictionary Count

Get Length of Nested Dictionary in Python

Dictionaries can also contain other dictionaries as values. To find the length of such nested dictionaries, you’ll need to navigate through each level.

Here is an example.

usa_data = { "California": {"Population": 39538223, "Area": 163696}, "Texas": {"Population": 29145505, "Area": 268596}, "Florida": {"Population": 21538187, "Area": 65758} } # Length of the outer dictionary print(len(usa_data)) # Output: 3 # Length of the nested dictionary for California print(len(usa_data["California"])) # Output: 2

Here, len(usa_data) returns 3 because there are three states in the outer dictionary. len(usa_data["California"]) returns 2 because there are two key-value pairs within the nested dictionary for California.

You will also get the exact output once you execute the above Python code. Check out the screenshot below:

python dictionary length

Conclusion

In this tutorial, I explained how to find the length of a dictionary in Python using the len() method and showed how to find the length of a nested dictionary in Python with examples.

You may also like:

Read Entire Article