Select Page

How to Find the Length of a List in Python

by | Programming, Python, Tips

The length of a Python list is the same as the number of items in the list. You can find the length of a list using the built-in len() function. You can also use a for loop to iterate over the list and count the number of items.

This tutorial will go through calculating the length of a list with examples.


What is a List in Python?

The Python list is one of four built-in data types used to store collections of data. The other three are dictionary, set, and tuple. You can create a list using square brackets:

a_list = [10, 20, 30]

print(a_list)
[10, 20, 30]

A Python list has the following qualities:

  • Ordered: the items have a defined order which does not change. If you add new items to a list, they will be placed at the end of the list.
  • Changeable: you can change, add, and remove items from a list.
  • Indexed: items in a list are indexed with integer values, starting with 0 and increasing by 1 for each following item.
  • Allows duplicates: since lists are indexed, they can have items with the same value

Finding the Length of a List Using the len() Function

The len() function returns the number of items in an object.

Syntax:

len(object)

object: must be a sequence or a collection

We can use the len() function to return the number of items in a list, which is the same as the length of the list. Let’s look at an example:

drinks_list = ["coca-cola", "sprite", "pepsi", "fanta"]

x = len(drinks_list)

print(f'The length of the list is: {x}')

We have a list of strings in the above code, which we pass to the len() function. We then print the length of the list to the console. Let’s run the code to get the result:

The length of the list is: 4

The list is four items long.

Time Complexity of len()

The time complexity of len() on lists is O(1). The len() function calls the method __len__(), which is in the predefined classes of iterable data structures. The __len__() method returns the size of a container as it increases or decreases depending on the action. Therefore, when you call the len() function, the interpreter is not finding the length but prints the value returned by the call to __len__(). As a result, the len() function runs in Python with O(1) complexity.

Finding the Length of a List Using the Naive Method

We can use a for loop to find the length of a list. We commonly refer to this method as the Naive method. Let’s look at an example:

drinks_list = ["coca-cola", "sprite", "pepsi", "fanta"]

counter = 0

for i in drinks_list:

    counter += 1

print(f'The length of the list using a for loop is {counter}')

In the above code, we define a counter that increases by one for every iteration over the list. Once the for loop is complete, the counter will have the total number of items in the list, which we print to the console. Let’s run the code to get the result:

The length of the list using a for loop is 4

Finding the Length of a List Using length_hint()

We can use the method length_hint() from the operator module to get the length of a list.

Syntax:

length_hint(object, default = 0)

object: must be a sequence or a collection

default: length_hint returns an estimated length for the object, it will first try to return the actual length, then an estimate using object.__length__hint(), and finally return the default value.

Let’s look at an example:

from operator import length_hint

drinks_list = ["coca-cola", "sprite", "pepsi", "fanta"]

list_len = length_hint(drinks_list)

print(f'The length of the list using a for loop is {list_len}')

Let’s run the code to get the result:

The length of the list using a for loop is 4

Summary

Congratulations on reading to the end of this tutorial! You can calculate the length of a list using the Naive method (for loop), len() method, or the length_hint() method. The len() is the most common and easiest method to use.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!