• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Filter Lists in Python

How to Filter Lists in Python

August 21, 2022 Leave a Comment

To filter a list in Python, the easiest way is with list comprehension.

lst = [0, 1, 2, 3, 4, 5]

filtered = [x for x in lst if x < 2]

print(filtered)

#Output:
[0, 1]

You can also use the Python filter() function with a lambda expression.

lst = [0, 1, 2, 3, 4, 5]

filtered = list(filter(lambda x: x < 2, lst))

print(filtered)

#Output:
[0, 1]

One other way to filter a list in Python is with a for loop.

lst = [0, 1, 2, 3, 4, 5]

filtered = []

for x in lst:
    if x < 2:
        filtered.append(x)

print(filtered)

#Output:
[0, 1]

When working with collections of data, the ability to modify and remove elements from your data based on conditions easily is valuable.

One such case is if you want to filter a list in Python.

Filtering lists is extremely common. There are a few ways you can filter a list in Python.

In our opinion, the easiest way to filter a list in Python is with list comprehension. The idea here is that you use list comprehension and if statement to filter items out of a list.

Below shows you how you can use list comprehension to filter a list in Python.

lst = [0, 1, 2, 3, 4, 5]

filtered = [x for x in lst if x < 2]

print(filtered)

#Output:
[0, 1]

How to Filter List with filter() Function in Python

Another way you can filter lists in Python is with the filter() function.

filter() allows you to filter iterable objects in Python with a lambda expression.

To use filter(), you pass a lambda expression and a list. Then filter() will return a filter object. Then, you convert the filter object to list with list().

Below shows you how to use filter() to filter a list in Python.

lst = [0, 1, 2, 3, 4, 5]

filtered = list(filter(lambda x: x < 2, lst))

print(filtered)

#Output:
[0, 1]

How to Filter List with for Loop in Python

One last way you can filter lists in Python is with a for loop.

Using loops to filter a list is the most basic way and can be more lines of code than if you used one of the examples above.

However, using a loop to filter a list in Python is valid and allows you more flexibility when adding items to the filtered list.

The process when using a for loop is that you first initialize an empty list and then you loop over each element. If a certain condition is met, you add the element to the empty list of filtered items.

Below shows you how to use a for loop to filter a list in Python.

lst = [0, 1, 2, 3, 4, 5]

filtered = []

for x in lst:
    if x < 2:
        filtered.append(x)

print(filtered)

#Output:
[0, 1]

Hopefully this article has been useful for you to learn how you can filter a list in Python.

Other Articles You'll Also Like:

  • 1.  How to Remove NaN from List in Python
  • 2.  Python Add Months to Datetime Variable Using relativedelta() Function
  • 3.  Convert String to Float with float() in Python
  • 4.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 5.  Calculating Sum of Squares in Python
  • 6.  Python Increment Counter with += Increment Operator
  • 7.  Check if Word is Palindrome Using Recursion with Python
  • 8.  pandas tail – Return Last n Rows from DataFrame
  • 9.  PROC REG Equivalent in Python
  • 10.  Using Python to Create Empty DataFrame with pandas

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy