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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.