# How to Filter List Elements in Python?

The `filter()` method filters the given sequence of elements with the help of a function that tests each element in the sequence to be true or not.

Syntax:

filter(function, sequence)

Parameters:

• function: the function that tests if each element of a sequence true or not.
• sequence: the sequence which needs to be filtered. It can be any iterable object such as sets, lists, tuples, or containers.

Returns: The method returns an iterator that is already filtered.

## Example – Filter a Python List with filter()

``````def even(x):
if x%2==0:
return True
else:
return False

# creating a list in Python
mylist= [3,5,1,5,7,8,2,6]

# using filter function to find even numbers
filtered = filter(even, mylist)

print('The even numbers are:')
for s in filtered:
print(s)
``````

Output

``````The even numbers are:
8
2
6``````

## Example – Python Filter List with Lambda

Alternatively, we can use a lambda function statement to create the function to pass it as an argument to the` filter()` method.

``````# creating a list in Python
mylist= [3,5,1,5,7,8,2,6]

#filter all the odd numbers using list Comprehension
result = [x for x in mylist if x%2!=0]
print('The odd numbers are ',list(result))

#filter all the even numbers using list Comprehension
result = [x for x in mylist if x%2==0]
print('The even numbers are ',list(result))``````

Output

``````The odd numbers are  [3, 5, 1, 5, 7]
The even numbers are  [8, 2, 6]``````

## Example – Filter with List Comprehension

The best way to filter the list elements is using the list comprehension statement `[x for x in list if condition]`. The condition can be replaced with any function which can be used as a filtering condition.

``````# creating a list in Python
mylist= [3,5,1,5,7,8,2,6]

#filter all the odd numbers using lambda
result = filter(lambda x: x % 2 != 0, mylist)
print('The odd numbers are ',list(result))

#filter all the even numbers using lambda
result = filter(lambda x: x % 2 == 0, mylist)
print('The even numbers are ',list(result))``````

Output

``````The odd numbers are  [3, 5, 1, 5, 7]
The even numbers are  [8, 2, 6]``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## Python Program to Measure the Elapsed Time in Python

There are multiple ways to measure the elapsed time in Python. The modules that are available are time, timeit, and Datetime to measure elapsed time. Using time Module We can use the time module… ## Python syntaxerror: eol while scanning string literal

Table of Contents Hide What does EOL while scanning string literal error mean in Python?Missing quotation marksString spanning multiple lines using the wrong syntaxMismatch of quotesUsing backslash before the ending… ## How to rename columns in Pandas DataFrame

Table of Contents Hide Method 1: Rename Specific column names in Pandas DataFrameMethod 2: Rename all column names in Pandas DataFrameMethod 3: Replace specific characters in Columns of Pandas DataFrame… ## Python String strip()

Table of Contents Hide SyntaxParametersReturn ValueExample: Working of the strip() methodExample 2: Python strip() method usage in actual program The Python String strip() method is a built-in function that strips… ## Python String rstrip()

Table of Contents Hide SyntaxParametersReturn Value Example 1: Working with rstrip() methodExample 2 – How to use rstrip() method in the real world? The Python String rstrip() method is a… ## Python Delete File – Step-by-Step Guide

Table of Contents Hide Methods to Delete Files in PythonMethod 1 – Using os ModuleDelete a fileDelete a directoryMethod 2 – Using shutil moduleMethod 3 – Using pathlib moduleRemove a…