As a developer working on a project for one of my clients, I recently encountered a situation where I needed to determine the number of elements in a list. After researching and experimenting with different methods, I discovered several ways to accomplish this task effectively. In this tutorial, I will explain how to find the length of a list in Python.
Find the Length of a List in Python
Before getting into the methods for finding the length of a list, let’s briefly review what lists are in Python. A list is a built-in data structure that allows you to store and organize multiple items in a single variable. Lists are mutable, meaning you can modify, add, or remove elements after the list is created. In Python, lists are defined using square brackets [] and elements are separated by commas.
Here’s an example of a list containing names of US states:
states = ["California", "New York", "Texas", "Florida", "Illinois"]Read How to Get the Last Element of a List in Python?
Method 1: Use the len() Function
The most common and efficient way to find the length of a list in Python is by using the built-in len() function. The len() function takes a list as an argument and returns the number of elements in that list. The len() method is the most commonly used method to find the length of the list in Python.
Here’s an example that demonstrates how to use len():
us_cities = ["New York City", "Los Angeles", "Chicago", "Houston", "Phoenix"]
length = len(us_cities)
print("The length of the list is:", length)Output:
The length of the list is: 5I executed the above example code and added the screenshot below.

In this example, we have a list called us_cities that contains the names of major cities in the United States. By passing us_cities to the len() function, we get the length of the list, which is 5.
Check out How to Split a List in Python?
Method 2: Use a Loop
Another way to find the length of a Python list is by using a loop to iterate over the elements and keep track of the count. This method is less efficient compared to using len(), but it can be useful in certain scenarios where you need to perform additional operations while counting the elements.
Here’s an example that shows how to find the length of a list using a loop:
us_presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe"]
count = 0
for president in us_presidents:
count += 1
print("The length of the list is:", count)Output:
The length of the list is: 5I executed the above example code and added the screenshot below.

In this example, we initialize a variable count to 0. Then, we use a for loop to iterate over each element in the us_presidents list. For each iteration, we increment the count variable by 1. After the loop finishes, the count variable will hold the total number of elements in the list.
Read How to Sort a List of Objects by Attribute in Python?
Method 3: Use the sum() Function with a Generator Expression
Another interesting approach to finding the length of a list is by using the sum() function in combination with a generator expression. This method leverages the fact that True is considered as 1 and False as 0 in Python.
Here’s an example:
us_mountains = ["Mount Denali", "Mount Whitney", "Mount Elbert", "Mount Rainier", "Pikes Peak"]
length = sum(1 for _ in us_mountains)
print("The length of the list is:", length)Output:
The length of the list is: 5I executed the above example code and added the screenshot below.

In this example, we use a generator expression (1 for _ in us_mountains) inside the sum() function. The generator expression iterates over each element in the us_mountains list and yields 1 for each element. The sum() function then adds up all the 1s, effectively counting the number of elements in the list.
Read How to Print a List Without Brackets in Python?
Handle Empty Lists
When working with lists, it’s important to consider the case of empty lists. An empty list is a list that contains no elements. To get the length of a list in Python, the most common way to do so is to use the len() method as it’s the most efficient and is a built-in function.
Here’s an example that demonstrates finding the length of an empty list:
empty_list = []
length = len(empty_list)
print("The length of the empty list is:", length)Output:
The length of the empty list is: 0As expected, the len() function returns 0 for an empty list.
Check out How to Generate a List of Random Numbers in Python?
Example: Analyze US State Populations
To illustrate the practical application of finding the length of a list, let’s consider a real-world example. Suppose you are working on a project that involves analyzing the populations of US states. You have a list that contains the populations of each state.
state_populations = [39538223, 19454279, 29145505, 21538187, 12812508]To determine the number of states in your analysis, you can simply find the length of the state_populations list using the len() function:
num_states = len(state_populations)
print("Number of states in the analysis:", num_states)Output:
Number of states in the analysis: 5This information can be useful for various purposes, such as validating data consistency, allocating resources, or generating reports.
Read How to Slice Lists in Python?
Conclusion
In this tutorial, I helped you to learn how to find the length of a list in Python. I explained three methods to accomplish this task, such as using len() function , using a loop , using the sum() function with a general expression. I also discussed how to handle empty lists and a real-world example to analyze US state populations.
You may read:
- How to Clear a List in Python?
- How to Iterate Through a List Backward in Python?
- How to Concatenate a List of Strings into a Single String in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.