Python Filter Data from a List Example
Filtering a list means creating a new list that keeps only the items you want.
This example shows simple beginner-friendly ways to filter data from a Python list using:
- a
forloop - an
ifstatement - a list comprehension
If you are new to loops and conditions, this is a good place to start before reading more detailed guides on Python for loops and Python if statements.
Quick example
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)
Output:
[2, 4, 6]
Use a list comprehension when you want to create a new list that keeps only matching items.
What this example shows
- How to create a new list from an existing list
- How to keep only items that match a condition
- How filtering does not change the original list unless you reassign it
- When to use a loop vs a list comprehension
Example 1: Filter with a for loop
A for loop is often the easiest way to understand filtering.
The basic idea is:
- Start with an empty list
- Loop through the original list
- Check each item with an
ifstatement - Add matching items to the new list
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = []
for number in numbers:
if number % 2 == 0:
even_numbers.append(number)
print("Original list:", numbers)
print("Filtered list:", even_numbers)
Output:
Original list: [1, 2, 3, 4, 5, 6]
Filtered list: [2, 4, 6]
Why this works
numbersis the original listeven_numbers = []creates an empty result listfor number in numbers:checks each item one by oneif number % 2 == 0:keeps only even numberseven_numbers.append(number)adds matching items to the result
The key line is:
if number % 2 == 0:
That condition returns True only for even numbers.
If you want to learn more about adding items to a list, see Python list.append().
Example 2: Filter with a list comprehension
After you understand the loop version, you can write the same logic in one line with a list comprehension.
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [number for number in numbers if number % 2 == 0]
print("Original list:", numbers)
print("Filtered list:", even_numbers)
Output:
Original list: [1, 2, 3, 4, 5, 6]
Filtered list: [2, 4, 6]
This version is shorter, but it does the same job:
- loop through
numbers - test each item
- keep only matching items
Use a list comprehension when the condition is short and easy to read.
For more practice, read how to use list comprehensions in Python.
Common filtering conditions
You can filter lists in many ways. Here are a few simple patterns.
Numbers greater than a value
numbers = [3, 8, 12, 1, 20]
result = [n for n in numbers if n > 10]
print(result)
Output:
[12, 20]
Even or odd numbers
numbers = [1, 2, 3, 4, 5, 6]
odd_numbers = [n for n in numbers if n % 2 != 0]
print(odd_numbers)
Output:
[1, 3, 5]
Strings that contain a word
words = ["apple pie", "banana", "green apple", "orange"]
result = [word for word in words if "apple" in word]
print(result)
Output:
['apple pie', 'green apple']
Strings that start with a letter
names = ["Alice", "Bob", "Anna", "Charlie"]
result = [name for name in names if name.startswith("A")]
print(result)
Output:
['Alice', 'Anna']
Items that are not empty
items = ["book", "", "pen", "", "paper"]
result = [item for item in items if item != ""]
print(result)
Output:
['book', 'pen', 'paper']
Expected output and why it works
When you filter a list, it helps to print both the original list and the new list.
names = ["Anna", "Ben", "Alex", "Cara"]
filtered_names = []
for name in names:
if name.startswith("A"):
filtered_names.append(name)
print("Original list:", names)
print("Filtered list:", filtered_names)
Output:
Original list: ['Anna', 'Ben', 'Alex', 'Cara']
Filtered list: ['Anna', 'Alex']
Why it works:
- The original list stays the same
- Only matching items are added to
filtered_names - The condition is the part that decides what stays
In this example, the important line is:
if name.startswith("A"):
Only names that start with "A" are added to the new list.
If you want a step-by-step guide for this task, see how to filter a list in Python.
Beginner mistakes to avoid
Here are some common problems when filtering lists.
Forgetting to create a new list
This will fail because result does not exist yet:
numbers = [1, 2, 3]
for number in numbers:
if number > 1:
result.append(number)
Create the list first:
numbers = [1, 2, 3]
result = []
for number in numbers:
if number > 1:
result.append(number)
print(result)
Using = instead of ==
This is wrong inside a condition:
if number = 2:
Use == to compare values:
if number == 2:
Expecting the original list to change automatically
Filtering usually creates a new list.
numbers = [1, 2, 3, 4]
result = [n for n in numbers if n > 2]
print(numbers)
print(result)
Output:
[1, 2, 3, 4]
[3, 4]
The original list is still [1, 2, 3, 4].
Writing a condition that returns the wrong type
Conditions should evaluate to True or False.
Good:
if number > 3:
Less useful for filtering logic:
if number + 3:
That second example produces a value, but it does not clearly express the rule you want.
Confusing filtering with sorting
Filtering removes items that do not match a condition.
Sorting changes the order of items.
These are different tasks.
Common causes of filtering problems
Many beginner errors come from a small number of causes:
- Trying to remove items from the same list while looping over it
- Using
append()on a variable that was not created as a list - Writing an invalid condition inside the loop or comprehension
- Mixing numbers and strings in a comparison without conversion
For example, this can cause problems:
items = [1, "2", 3]
for item in items:
if item > 1:
print(item)
In this list, some items are numbers and one is a string. Python cannot always compare them directly in a useful way.
Debugging tips
If your filtering code does not work, print values as the loop runs.
Useful debug lines:
print(items)
print(item)
print(result)
print(type(item))
print(item % 2 == 0)
Example:
numbers = [1, 2, 3, 4]
result = []
for item in numbers:
print("Current item:", item)
print("Is even?", item % 2 == 0)
if item % 2 == 0:
result.append(item)
print("Final result:", result)
This helps you see:
- what each item is
- whether the condition is
TrueorFalse - what gets added to the result list
FAQ
What is the easiest way to filter a list in Python?
For beginners, use a for loop with an if statement. After that, learn the list comprehension version.
Does filtering change the original list?
Not unless you assign the result back to the same variable. Usually filtering creates a new list.
Should I use filter() for this page?
No. This example focuses on loops and list comprehensions because they are easier for beginners to read.
Can I filter a list of strings?
Yes. You can keep only strings that match a condition, such as containing a word or starting with a letter.
See also
- How to filter a list in Python
- How to use list comprehensions in Python
- Python if statements explained
- Python for loops explained
- Python list append() method
Try changing the examples in this page to make your own filters:
- keep numbers greater than
10 - keep only odd numbers
- keep strings that contain
"cat" - keep names that start with
"B"
That is the main skill: write a condition, test each item, and keep only the ones that match.