Get length of a python dictionary

Find Length of a Python Dictionary

A dictionary is a data structure in Python used to store key-value pairs. The length of a dictionary is the number of key-value pairs present in the dictionary. In this tutorial, we will look at how to find the length of a dictionary in Python with the help of some examples.

How to get the length of a Python dictionary?

You can use the Python built-in len() function to find the length of a dictionary. It returns the number of key-value pairs present in the dictionary.

The following is the syntax –

# length of dictionary
len(my_dict)

Let’s look at some examples of using the above syntax.

Let’s create a dictionary containing the scores in Science of some students in a test and then use the len() function to find the length of the dictionary.

# create a dictionary
science_scores = {
    "Tim": 87,
    "Emma": 91,
    "Mark": 68
}
# get length of dictionary
print(len(science_scores))

Output:

3

We find that the length of the dictionary “science_scores” is 3. That is, it contains 3 key-value pairs, which in this example tells us the number of students-score pairs present in the dictionary.

Note that the length of a dictionary is the same as the length of the dictionary’s keys and values.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# length of dictionary
print(len(science_scores))
# length of dictionary keys
print(len(science_scores.keys()))
# length of dictionary values
print(len(science_scores.values()))

Output:

3
3
3

You can see that the length of the dictionary’s keys and values is the same as that of the dictionary.

Let’s look at another example.

# create a dictionary
science_scores = {}
# get length of dictionary
print(len(science_scores))

Output:

0

Here we see that the length for an empty dictionary is 0.

Length for a specific key in a dictionary

Let’s say instead of just the score in Science, you have the scores in three subjects – Science, Math, and English in a list for each student.

Now, if you just want to find the length of the value for a particular key, you can use the len() function as follows –

# create a dictionary
scores = {
    "Tim": [87, 74, 63],
    "Emma": [91, 82, 88],
    "Mark": [68, 91, 57]
}
# get length of a key's value
print(len(scores["Tim"]))

Output:

3

Here we first access the value for the key “Tim” and then find its length using the len() function. This gives us the number of scores present for “Tim”.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top