DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

2 1

How to Filter List Elements in Python?

ItsMyCode |

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()

# function that filters vowels
from os import truncate

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)

Enter fullscreen mode Exit fullscreen mode

Output

The even numbers are:
8
2
6
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

The odd numbers are [3, 5, 1, 5, 7]
The even numbers are [8, 2, 6]
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

The odd numbers are [3, 5, 1, 5, 7]
The even numbers are [8, 2, 6]
Enter fullscreen mode Exit fullscreen mode

The post How to Filter List Elements in Python? appeared first on ItsMyCode.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay