How to Find Length of List in Python

python length of list

Finding the length of a list is a common task. It helps you check if a list is empty, iterate through all its elements, or perform actions based on its size. Python offers simple, direct ways to find a list’s length.

Knowing the length helps you manage data, control loops, and ensure your program works correctly.

What is List Length?

The length of a Python list is simply the number of items it contains. For example, a list [1, 2, 3] has a length of 3. An empty list [] has a length of 0.

You need to find the length to:

  • Know how many elements are in your list.
  • Control loops that process list items.
  • Check for empty lists before performing operations.
Academy Pro

Python Programming Course

In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.

11.5 Hrs
51 Coding Exercises
Learn Python Programming

2 Main Ways to Find List Length in Python

You can find the length of a list in two primary ways:

  • Using the len() function: This is the most common and recommended way.
  • Using a loop (less common but illustrates concepts): You can count elements by iterating through the list.

Let’s look at each one.

The len() function is a built-in Python function. It takes an object as an argument and returns its length (the number of items in it). This function works for many sequence types, like strings, tuples, and lists.

It is the simplest and most efficient way to get a list’s length.

Here’s how to use it:

# An empty list
my_empty_list = []
print(f"Length of empty list: {len(my_empty_list)}")

# A list with numbers
my_numbers = [10, 20, 30, 40, 50]
print(f"Length of numbers list: {len(my_numbers)}")

# A list with mixed data types
my_mixed_list = ["apple", 123, True, 3.14]
print(f"Length of mixed list: {len(my_mixed_list)}")

# A nested list
my_nested_list = [[1, 2], [3, 4, 5], 6]
print(f"Length of nested list: {len(my_nested_list)}")

This code outputs:

Length of empty list: 0
Length of numbers list: 5
Length of mixed list: 4
Length of nested list: 3

Key Points about len():

  • It works for all list types, including empty lists.
  • It is very fast, even for large lists.
  • It returns an integer representing the count of top-level items. For nested lists, it counts the sub-lists as single items.

You should always use len() when you need to find the length of a list. It is Pythonic and efficient.

2. Using a Loop (Counting Manually)

While len() is the best way, you can also find a list’s length by looping through it and counting each item. This method is generally not recommended for finding list length, but it helps you understand how counting works.

You initialize a counter variable to zero. Then, you iterate through each item in the list and add one to the counter for each item.

Here’s an example using a for loop:

my_list = ['red', 'green', 'blue', 'yellow']
count = 0

for item in my_list:
    count += 1 # Add 1 for each item found

print(f"Length found by loop: {count}")

This code outputs:

Length found by loop: 4

This approach works, but it is much slower than len() for large lists. It also adds more code for a simple task. You would only use this if you were forbidden from using len() or if you needed to do other operations while counting.

Another way to think about this is using list comprehensions with sum() (which is still a form of looping):

my_list = ['A', 'B', 'C']
# Create a list of 1s, one for each item, then sum them
length = sum(1 for _ in my_list)
print(f"Length found by sum of 1s: {length}")

This code outputs:

Length found by sum of 1s: 3

This is also an inefficient way compared to len(). The underscore _ is a common convention for a variable you do not intend to use.

Why len() is the Best Way

  • Efficiency: The len() function is implemented in C for CPython (the standard Python interpreter). This means it is very fast. It does not actually count items one by one. Python lists store their size internally, so len() just retrieves this stored value.
  • Readability: len(my_list) is clear and easy to understand.
  • Simplicity: It is a single function call.
Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Academy Pro Subscription

Grab 50% off
on Top Courses - Free Trial Available

×
Scroll to Top