How to Filter None Values from a Mixed List in Python

Filter None values from a mixed Python list using a list comprehension with the `is not None` condition.

Easy Python 3.9+ Aug 9, 2026 Lists & loops 14 views 0 copies

Python code

7 lines
Python 3.9+
mixed_list = [1, None, "hello", None, 3.14, None, [1, 2], None]

filtered_list = [item for item in mixed_list if item is not None]

print(f"Original list: {mixed_list}")
print(f"Filtered list: {filtered_list}")
print(f"Original length: {len(mixed_list)}, Filtered length: {len(filtered_list)}")

Output

stdout
Original list: [1, None, 'hello', None, 3.14, None, [1, 2], None]
Filtered list: [1, 'hello', 3.14, [1, 2]]
Original length: 8, Filtered length: 4

How it works

The list comprehension [item for item in mixed_list if item is not None] iterates over each element and keeps only those that are not None. The is not None check is preferred over != None because it checks identity rather than equality, which is faster and more explicit. This approach preserves the original list and creates a new filtered list, leaving the original untouched. It works for any type of elements—integers, strings, floats, lists, and more—because it only filters out None itself.

Common mistakes

  • Using `if item` instead of `if item is not None` — this filters out falsy values like 0, '', and [] as well.
  • Forgetting that `None` is a singleton — using `== None` works but is less idiomatic.
  • Modifying the list while iterating over it, which can lead to skipped items or unexpected behavior.

Variations

  1. Use `filter(lambda x: x is not None, mixed_list)` to get a filter object, then convert to a list with `list()`.
  2. Use a `for` loop with `append` to build the filtered list for extra clarity in teaching contexts.

Real-world use cases

  • Cleaning API responses that may contain null fields before processing the data further.
  • Removing missing values from database query results before aggregating statistics.
  • Filtering out placeholder entries in configuration lists loaded from JSON files.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Lists & loops

Related tutorials and quizzes for this topic.