Last modified: May 25, 2026

Python List Length Check with If

Working with lists is a core part of Python programming. One common task is checking the size of a list before performing an action. You can easily do this by combining the len() function with an if statement.

This article will show you how to use Python list length with if. You will learn simple patterns. You will see real code examples. This guide is perfect for beginners.

Why Check List Length?

Checking the length of a list helps you avoid errors. For example, trying to access an index that does not exist will crash your program. Using an if statement with len() prevents this.

It also helps you make decisions. You can run different code based on how many items are in your list. This makes your programs smarter and safer.

Basic Syntax

The len() function returns the number of items in a list. You can use it inside an if statement like this:


my_list = [10, 20, 30]
if len(my_list) > 0:
    print("The list has items.")

The list has items.

You can also check if a list is empty. An empty list has a length of zero.


empty_list = []
if len(empty_list) == 0:
    print("The list is empty.")

The list is empty.

Common Use Cases

Check Before Accessing an Element

Always check the length before you access an index. This prevents IndexError.


fruits = ["apple", "banana", "cherry"]
index = 5

if index < len(fruits):
    print(fruits[index])
else:
    print("Index is out of range.")

Index is out of range.

Perform Actions Based on Size

You can run different code for small and large lists.


numbers = [1, 2, 3, 4, 5]

if len(numbers) < 3:
    print("Small list")
elif len(numbers) < 10:
    print("Medium list")
else:
    print("Large list")

Medium list

Looping with Length Check

Sometimes you want to loop while a list has items. This is useful for processing queues or task lists.


tasks = ["task1", "task2", "task3"]

while len(tasks) > 0:
    current = tasks.pop(0)
    print(f"Processing {current}")

Processing task1
Processing task2
Processing task3

Using if not for Empty Lists

Python has a cleaner way to check if a list is empty. You can use if not directly on the list. This works because an empty list is considered falsy.


my_list = []
if not my_list:
    print("List is empty")
else:
    print("List has items")

List is empty

This is more readable than len(my_list) == 0. However, use len() when you need the exact number.

Combining with Other List Methods

You can use length checks with list methods like insert() or extend(). For example, before inserting an item at a specific position, check if the index is valid.

For a full guide on inserting items, see our Python List Insert: Complete Guide.


items = [1, 2, 3]
position = 1
value = 99

if position <= len(items):
    items.insert(position, value)
    print(items)
else:
    print("Position is out of range")

[1, 99, 2, 3]

Performance Considerations

The len() function is very fast. It runs in constant time, O(1). This means it does not matter how big your list is. Checking length is always quick.

This makes it safe to use in loops and conditionals. You do not need to worry about slowing down your program.

Real-World Example: User Input Validation

Here is a practical example. Imagine you are building a simple to-do app. You want to show a message based on how many tasks are left.


todo_list = ["buy milk", "call mom", "finish homework"]

if len(todo_list) == 0:
    print("All tasks done! Great job!")
elif len(todo_list) < 5:
    print(f"You have {len(todo_list)} tasks. Keep going!")
else:
    print("You have many tasks. Focus on the most important.")

You have 3 tasks. Keep going!

Common Mistakes to Avoid

One common mistake is forgetting to call len() as a function. Always use parentheses.


# Wrong
if len my_list > 5:
    print("Too many items")

# Correct
if len(my_list) > 5:
    print("Too many items")

Another mistake is comparing length to True or False. Always compare to a number.

Related Topics

If you often work with lists, you might find these guides useful:

Conclusion

Using Python list length with if statements is a simple but powerful technique. It helps you write safer and more readable code. Always check the length before accessing an index. Use if not for empty list checks. Remember that len() is fast and reliable.

Practice these patterns in your own projects. You will soon use them without thinking. Happy coding!