Find Length of List in Python (3 Examples)

 

Hi! This tutorial will demonstrate how to determine the length of a list in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Example List

We will create an example Python list of strings whose length will be calculated in this tutorial. Therefore, in your preferred Python coding IDE, run the line of code below to create the example list of strings:

my_list = ["apple","orange","pear","banana","grape"]
 
print(my_list)
 
# ['apple', 'orange', 'pear', 'banana', 'grape']
 
 
print(type(my_list))
 
# <class 'list'>

Now that the example list has been created, we will look at examples of how to get its length.
 

Example 1: Get Length of List Using len() Function

In this first example, we will use Python’s len() function to find the length of the list:

list_length = len(my_list)
 
print(list_length)
 
# 5
 
print(type(list_length))
 
# <class 'int'>

The len() function in the above example simply counts the number of elements in the list and returns it. Using this function is a very easy and quick way to get the length of a list in Python.
 

Example 2: Get Length of List Using List Comprehension & sum() Function

In this next example, we will use list comprehension and the sum() function to calculate the length of the list:

list_length = sum(1 for x in my_list)
 
print(list_length)
 
# 5
 
print(type(list_length))
 
# <class 'int'>

We used list comprehension inside the sum() function to run an iteration through the elements in the list, which were then counted.

Since the sum() function can only take integer values, we used 1 to represent the count of the elements inside the list.
 

Example 3: Get Length of List Using For Loop

In this last example, we will use a for loop to determine the length of the list:

list_length = 0
 
for i in my_list:
  list_length += 1
 
print(list_length)
 
# 5
 
print(type(list_length))
 
# <class 'int'>

In the above example, the for loop iterates through all the elements inside the list and stops when it gets to the last element.

We used the list_length variable, whose initial value is set to 0, to count the number of the items in the list by incrementing its value by one inside the for loop.

This means that with each iteration, the value of list_length increases by 1. Note that in Python, list_length += 1 is the same as list_length = list_length + 1.

With these simple examples, we have demonstrated how to find the length of a list in Python. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the length of a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the length of a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the length of a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top