05-10-2020, 04:27 AM
How to Filter a List of Lists in Python?
Short answer: To filter a list of lists for a condition on the inner lists, use the list comprehension statement and replace with your filtering condition that returns to include inner list , and otherwise.
Lists belong to the most important data structures in Python—every master coder knows them by heart! Surprisingly, even intermediate coders don’t know the best way to filter a list—let alone a list of lists in Python. This tutorial shows you how to do the latter!
Problem: Say, you’ve got a list of lists. You want to filter the list of lists so that only those inner lists remain that satisfy a certain condition. The condition is a function of the inner list—such as the average or sum of the inner list elements.
![[Image: filter-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/05/filter-1024x576.jpg)
Example: Given the following list of lists with weekly temperature measurements per week—and one inner list per week.
How to filter out the colder weeks with average temperature value <8? This is the output you desire:
There are two semantically equivalent methods to achieve this: list comprehension and the function. Let’s explore both variants next.
If you’re short on time, you can also get a quick overview by playing with the code in your web browser—I’ll explain the code after that.
https://repl.it/@finxter/filterlistoflis...?lite=true
Method 1: List Comprehension
The most Pythonic way of filtering a list—in my opinion—is the list comprehension statement. You can replace with any function of you would like to use as a filtering condition. Only elements that are in the and meet the are included in the newly created list.
Solution: Here’s how you can solve the above problem to filter a list of lists based on a function of the inner lists:
The second and third list in the list of lists meet the condition of having an average temperature of less than 8 degrees. So those are included in the variable.
You can visualize the memory usage of this code snippet in the following interactive tool:
https://pythontutor.com/iframe-embed.htm...nces=false
This is the most efficient way of filtering a list and it’s also the most Pythonic one. If you look for alternatives though, keep reading.
Related articles:
Method 2: Filter() Function
The function takes a function as input that takes on argument (a list element) and returns a Boolean value that indicates whether this list element should pass the filter. All elements that pass the filter are returned as a new object (a filter object).
You can use the function statement to create the function right where you pass it as an argument. The syntax of the lambda function is and it means that you use as an input argument and you return expression as a result (that can or cannot use to decide about the return value). For more information, see my detailed blog article about the lambda function.
Again, the second and third list in the list of lists meet the condition of having an average temperature of less than 8 degrees. So those are included in the variable.
The function returns a filter object that’s an . To convert it to a list, you use the constructor.
Play with this code by clicking “Next” in the interactive code visualization tool:
https://pythontutor.com/iframe-embed.htm...nces=false
Related articles:
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
Short answer: To filter a list of lists for a condition on the inner lists, use the list comprehension statement
Code:
[x for x in list if condition(x)]Code:
condition(x)Code:
TrueCode:
xCode:
FalseLists belong to the most important data structures in Python—every master coder knows them by heart! Surprisingly, even intermediate coders don’t know the best way to filter a list—let alone a list of lists in Python. This tutorial shows you how to do the latter!
Problem: Say, you’ve got a list of lists. You want to filter the list of lists so that only those inner lists remain that satisfy a certain condition. The condition is a function of the inner list—such as the average or sum of the inner list elements.
![[Image: filter-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/05/filter-1024x576.jpg)
Example: Given the following list of lists with weekly temperature measurements per week—and one inner list per week.
Code:
# Measurements of a temperature sensor (7 per week)
temperature = [[10, 8, 9, 12, 13, 7, 8], # week 1 [9, 9, 5, 6, 6, 9, 11], # week 2 [10, 8, 8, 5, 6, 3, 1]] # week 3How to filter out the colder weeks with average temperature value <8? This is the output you desire:
Code:
print(cold_weeks)
# [[9, 9, 5, 6, 6, 9, 11], [10, 8, 8, 5, 6, 3, 1]]There are two semantically equivalent methods to achieve this: list comprehension and the
Code:
map()If you’re short on time, you can also get a quick overview by playing with the code in your web browser—I’ll explain the code after that.
https://repl.it/@finxter/filterlistoflis...?lite=true
Method 1: List Comprehension
The most Pythonic way of filtering a list—in my opinion—is the list comprehension statement
Code:
[x for x in list if condition]Code:
conditionCode:
xCode:
listCode:
conditionSolution: Here’s how you can solve the above problem to filter a list of lists based on a function of the inner lists:
Code:
# Measurements of a temperature sensor (7 per week)
temperature = [[10, 8, 9, 12, 13, 7, 8], # week 1 [9, 9, 5, 6, 6, 9, 11], # week 2 [10, 8, 8, 5, 6, 3, 1]] # week 3 # How to filter weeks with average temperature <8? # Method 1: List Comprehension
cold_weeks = [x for x in temperature if sum(x)/len(x)<8]
print(cold_weeks)
# [[9, 9, 5, 6, 6, 9, 11], [10, 8, 8, 5, 6, 3, 1]]The second and third list in the list of lists meet the condition of having an average temperature of less than 8 degrees. So those are included in the variable
Code:
cold_weeksYou can visualize the memory usage of this code snippet in the following interactive tool:
https://pythontutor.com/iframe-embed.htm...nces=false
This is the most efficient way of filtering a list and it’s also the most Pythonic one. If you look for alternatives though, keep reading.
Related articles:
- List Comprehension — Python List of Lists
- Filter() vs List Comprehension
- Nested List Comprehension
- The Ultimate Guide to Python Lists
- List Comprehension
Method 2: Filter() Function
The
Code:
filter(function, iterable)Code:
iterableYou can use the
Code:
lambdaCode:
lambda x: expressionCode:
xCode:
xCode:
# Measurements of a temperature sensor (7 per week)
temperature = [[10, 8, 9, 12, 13, 7, 8], # week 1 [9, 9, 5, 6, 6, 9, 11], # week 2 [10, 8, 8, 5, 6, 3, 1]] # week 3 # How to filter weeks with average temperature <8? # Method 2: Map()
cold_weeks = list(filter(lambda x: sum(x) / len(x) < 8, temperature))
print(cold_weeks)
# [[9, 9, 5, 6, 6, 9, 11], [10, 8, 8, 5, 6, 3, 1]]Again, the second and third list in the list of lists meet the condition of having an average temperature of less than 8 degrees. So those are included in the variable
Code:
cold_weeksThe
Code:
filter()Code:
iterableCode:
list(...)Play with this code by clicking “Next” in the interactive code visualization tool:
https://pythontutor.com/iframe-embed.htm...nces=false
Related articles:
- How to Filter Using the Lambda Function
- Which is Faster – List Comprehension or Map()?
- How to Filter Using the Lambda Function?
- Python Programming Tutorial
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!

