How to Determine the Length of the Dictionary in Python

Techwalla may earn compensation through affiliate links in this story.

In the Python programming language, the dictionary stores sets of data and is similar to arrays used in other languages, like C. For example, a dictionary in Python could contain the name, age and grade level of all students in a school. When programming in Python, you may need to calculate the number of items in the dictionary. Use the "Len" function to calculate the dictionary length.

Story Continues Below Advertisement

Step 1

Open your Python editor.

Video of the Day

Step 2

Create a dictionary. For example, type:

dictStudent = {'Name': 'Justin', 'Age': 7, 'Grade': 1};

Story Continues Below Advertisement

In this example, you are creating a dictionary with a student's name, age, and grade level.

Step 3

Determine the length of the dictionary by typing:

len (dictStudent)

Story Continues Below Advertisement

Step 4

Press "Enter." Python returns:


Continuing the example, Python returns three, because the dictionary contains three items; the name, age and grade for a student.