How to Find the List Length in Python

January 25, 2023

Introduction

Lists are a sequential data type in Python. This mutable array-like data structure is convenient for storing a collection of items.

Python has different methods to find the total number of elements in a list (or the list length). This article shows three ways to find the list length in Python.

How to Find the List Length in Python

Prerequisites

Finding the Length of a List in Python

Determining the length of a list in Python is a simple task that can be achieved in a variety of ways. Depending on the task at hand, you can use 3 methods to find the length of a list:

  1. Without functions, using programming logic.
  2. Using a built-in function (preferred).
  3. Importing an external function from a library.

The sections below explain all three methods through practical examples.

Method 1: Naïve Counter Method

The first way to determine a list length in Python is to use a for loop and a counter. This method does not require any built-in or imported functions.

The following code shows how to find the list length using a for loop and a counter:

my_list = [0,1,2,3,4]
counter = 0
for element in my_list:
  counter += 1
print(counter)
Python for loop counter list length terminal output

As the loop goes through the elements in the list, the counter increments by one each time. The final value of the counter is the list length.

Method 2: len()

Python offers a built-in method to find the length of a list called len(). This method is the most straightforward and common way to determine a list length.

To use the len() function, provide the list as an argument. For example:

my_list = [0,1,2,3,4]
print(len(my_list))
len() list length terminal output

The built-in len() function returns the element count as an integer.

Note: Methods such as len() and slicing also work on strings and tuples in Python.

Method 3: length_hint()

The Python operator library contains a method called length_hint() for estimating the length of iterable objects. The method returns the exact length of lists and objects with a known length. Alternatively, it provides the estimated length.

To use the length_hint() function, import the method from the operator library and provide the list as input. For example:

from operator import length_hint
my_list = [0,1,2,3,4]
print(length_hint(my_list))
operator length_hint() list length terminal output

The function outputs the Python list length as an integer.

Conclusion

By reading this guide, you now know three different ways to find the length of a Python list. Knowing different methods provides flexibility in choosing the best one for your use case.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How to Append a String in Python
January 18, 2023

Python offers several different ways to append (or concatenate)...
Read more
String Slicing in Python
December 21, 2022

String slicing is a common task when working with long string values. Learn...
Read more
How to Substring a String in Python
December 13, 2022

Need to take out a substring from a string? Python...
Read more
Python: How to Add Items to Dictionary
November 10, 2022

Learn how to add new items to a Python dictionary in...
Read more