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.

Advertisement

Step 1

Open your Python editor.

Step 2

Create a dictionary. For example, type:

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

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:

Advertisement

len (dictStudent)

Step 4

Press "Enter." Python returns:

3

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

Advertisement

references