Last modified: Apr 22, 2026 By Alexander Williams

Check if List is Not Null in Python

Checking if a list is not null is a fundamental task. It prevents errors in your code. This guide explains the best methods.

You will learn several techniques. We cover simple truthy checks and explicit comparisons. Each method has its use case.

Understanding "Null" vs. "Empty" in Python

First, clarify the terms. In Python, "null" is represented by the None object. It signifies the absence of a value.

An "empty" list is a list object that exists but contains no items. It is written as [] or list().

A list can be None, empty, or populated. Your check must account for this.

Method 1: Using Truthy Evaluation (Recommended)

Python treats empty sequences as False in a Boolean context. Non-empty sequences are True.

This property gives us a concise way to check. Use the list directly in an if statement.


my_list = [1, 2, 3]

if my_list:
    print("List is not null and not empty.")
else:
    print("List is either None or empty.")

List is not null and not empty.

This works because my_list is truthy. It contains elements. The if block executes.

Now see what happens with an empty list.


empty_list = []

if empty_list:
    print("This won't print.")
else:
    print("List is empty (falsy).")

List is empty (falsy).

The empty list is falsy. The else block runs. This is the most Pythonic and readable method.

For more on basic list handling, see our Python List Operations Guide for Beginners.

Method 2: Explicit Check for None and Empty

Sometimes you need to distinguish between None and an empty list. Use explicit comparisons.

Combine checks with is not None and len(). This is very clear in intent.


def check_list_explicitly(input_list):
    if input_list is not None:
        if len(input_list) > 0:
            return "List is populated."
        else:
            return "List is empty but not None."
    else:
        return "List is None."

# Test cases
print(check_list_explicitly([10, 20]))  # Populated
print(check_list_explicitly([]))        # Empty
print(check_list_explicitly(None))      # None

List is populated.
List is empty but not None.
List is None.

This method is verbose. It is useful when the three states (None, empty, populated) require different logic.

Method 3: Using the len() Function

The len() function returns the number of items in a sequence. A length greater than zero means the list is not empty.

You must ensure the variable is not None first. Calling len() on None causes a TypeError.


my_data = ["apple", "banana"]

if my_data is not None and len(my_data) > 0:
    print(f"List has {len(my_data)} items.")
    # Access the first item safely
    print(f"First item: {my_data[0]}")

List has 2 items.
First item: apple

This pattern is common. It checks for existence and then for content. It prevents the Python List Index Out of Range Error when accessing elements.

Common Pitfalls and How to Avoid Them

Beginners often make mistakes. Let's look at two common ones.

Pitfall 1: Using `if my_list is not []`

This check is wrong. It tests if the variable is the exact empty list object. It will fail for a different empty list instance.


new_list = []  # This creates a new empty list object

if new_list is not []:  # This is True! They are different objects.
    print("This will always print, even though the list is empty.")

Always use truthy evaluation (if new_list:) or len(new_list) == 0 to check for emptiness.

Pitfall 2: Forgetting the None Check Before len()

This causes a runtime error. Always verify the object is not None.


result = None

# Wrong: This will crash
# if len(result) > 0:

# Correct: Check for None first
if result is not None and len(result) > 0:
    print("Safe!")
else:
    print("Result is None or empty.")

Defensive coding saves you from crashes. This is especially important when dealing with functions that may return None.

Practical Example: Processing User Data

Imagine a function that processes a list of usernames. The list might come from a database or an API.

You must handle all cases gracefully. Here is a robust implementation.


def process_users(user_list):
    """Safely processes a list of users."""
    if not user_list:  # Checks for None and empty in one go
        print("No user data to process.")
        return

    print(f"Processing {len(user_list)} users.")
    for user in user_list:
        print(f" - Hello, {user}!")

# Test with different inputs
print("Test 1:")
process_users(["Alice", "Bob", "Charlie"])

print("\nTest 2:")
process_users([])

print("\nTest 3:")
process_users(None)

Test 1:
Processing 3 users.
 - Hello, Alice!
 - Hello, Bob!
 - Hello, Charlie!

Test 2:
No user data to process.

Test 3:
No user data to process.

The condition if not user_list: is perfect here. It treats None and empty lists the same way, which is often the desired behavior.

When working with unique data, remember you can Convert Python List to Set to Remove Duplicates easily.

Best Practices Summary

Follow these rules for clean and safe code.

  • Use truthy evaluation by default.if my_list: is Pythonic and readable.
  • Use explicit is not None and len() checks only when you need to differentiate between None and an empty list.
  • Never use is or is not to compare to []. It does not check for emptiness correctly.
  • Check for None before calling len(). This prevents TypeErrors.
  • Consider the context. Often, treating None and an empty list identically simplifies logic.

Conclusion

Checking if a list is not null in Python is simple. The preferred method is truthy evaluation with if my_list:.

It handles both None and empty lists as falsy values. For cases requiring distinction, use explicit is not None and len() checks.

Understanding these techniques makes your code robust. It prevents common errors and is easy for others to read. Mastering list validation is a key step in becoming proficient with Python List Functions and data structures.

Start using these checks in your projects today. Your code will be more reliable and professional.