Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python filter()

#1
Python filter()

Python’s built-in filter() function is used to filter out elements that pass a filtering condition. It takes two arguments: function and iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that pass the filtering condition.

The filter() method has the following syntax:

filter(function, iterable)

Argument Description
function Assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not.
iterable Iterable from which to draw the elements to be filtered.
Return Value Iterator of filtered elements that pass the test posed by function argument.

Let’s dive into some examples of the filter() function:

Usage Examples: filter() a Python List


The filter(function, iterable) function takes a function as input that takes on argument (a list element) and returns a Boolean value whether this list element should pass the filter. All elements that pass the filter are returned as a new iterable object (a filter object).

You can use the lambda function statement to create the function right where you pass it as an argument. The syntax of the lambda function is lambda x: expression and it means that you use x as an input argument and you return expression as a result (that can or cannot use x to decide about the return value). For more information, see my detailed blog article about the lambda function.

lst = [8, 2, 6, 4, 3, 1] # Filter all elements <8
small = filter(lambda x: x<8, lst)
print(list(small)) # Filter all even elements
even = filter(lambda x: x%2==0, lst)
print(list(even)) # Filter all odd elements
odd = filter(lambda x: x%2, lst)
print(list(odd))

The output is:

# Elements <8
[2, 6, 4, 3, 1] # Even Elements
[8, 2, 6, 4] # Odd Elements
[3, 1]

The filter() function returns a filter object that’s an iterable. To convert it to a list, you use the list(...) constructor.

Related article:

Video: How to Filter a List in Python?


How can you filter a list in Python using an arbitrary condition? The following video will guide you through the different options:



Alternative: Filter with List Comprehension


The most Pythonic way of filtering a list—in my opinion—is the list comprehension statement [x for x in list if condition]. You can replace condition with any function of x you would like to use as a filtering condition.

For example, if you want to filter all elements that are smaller than, say, 10, you’d use the list comprehension statement [x for x in list if x<10] to create a new list with all list elements that are smaller than 10.

Here are three examples of filtering a list:

  • Get elements smaller than eight: [x for x in lst if x<8].
  • Get even elements: [x for x in lst if x%2==0].
  • Get odd elements: [x for x in lst if x%2].
lst = [8, 2, 6, 4, 3, 1] # Filter all elements <8
small = [x for x in lst if x<8]
print(small) # Filter all even elements
even = [x for x in lst if x%2==0]
print(even) # Filter all odd elements
odd = [x for x in lst if x%2]
print(odd)

The output is:

# Elements <8
[2, 6, 4, 3, 1] # Even Elements
[8, 2, 6, 4] # Odd Elements
[3, 1]

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 because I’ll explain to you each and every nuance of filtering lists in Python in this comprehensive guide.

Python Filter List Efficiently: filter() vs List Comprehension


[Spoiler] Which is faster to filter a list: filter() vs list comprehension? For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method.

To answer this question, I’ve written a short script that tests the runtime performance of filtering large lists of increasing sizes using the filter() and the list comprehension methods.

My thesis is that the list comprehension method should be slightly faster for larger list sizes because it leverages the efficient cPython implementation of list comprehension and doesn’t need to call an extra function.

I used my notebook with an Intel® Core™ i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM.

Then, I created 100 lists with both methods with sizes ranging from 10,000 elements to 1,000,000 elements. As elements, I simply incremented integer numbers by one starting from 0.

Here’s the code I used to measure and plot the results: which method is faster—filter() or list comprehension?

import time # Compare runtime of both methods
list_sizes = [i * 10000 for i in range(100)]
filter_runtimes = []
list_comp_runtimes = [] for size in list_sizes: lst = list(range(size)) # Get time stamps time_0 = time.time() list(filter(lambda x: x%2, lst)) time_1 = time.time() [x for x in lst if x%2] time_2 = time.time() # Calculate runtimes filter_runtimes.append((size, time_1 - time_0)) list_comp_runtimes.append((size, time_2 - time_1)) # Plot everything
import matplotlib.pyplot as plt
import numpy as np f_r = np.array(filter_runtimes)
l_r = np.array(list_comp_runtimes) print(filter_runtimes)
print(list_comp_runtimes) plt.plot(f_r[:,0], f_r[:,1], label='filter()')
plt.plot(l_r[:,0], l_r[:,1], label='list comprehension') plt.xlabel('list size')
plt.ylabel('runtime (seconds)') plt.legend()
plt.savefig('filter_list_comp.jpg')
plt.show()

The code compares the runtimes of the filter() function and the list comprehension variant to filter a list. Note that the filter() function returns a filter object, so you need to convert it to a list using the list() constructor.

Here’s the resulting plot that compares the runtime of the two methods. On the x axis, you can see the list size from 0 to 1,000,000 elements. On the y axis, you can see the runtime in seconds needed to execute the respective functions.

https://blog.finxter.com/wp-content/uplo...00x225.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" />

The resulting plot shows that both methods are extremely fast for a few tens of thousands of elements. In fact, they are so fast that the time() function of the time module cannot capture the elapsed time.

But as you increase the size of the lists to hundreds of thousands of elements, the list comprehension method starts to win:

For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method.

The reason is the efficient implementation of the list comprehension statement. An interesting observation is the following though. If you don’t convert the filter function to a list, you get the following result:

https://blog.finxter.com/wp-content/uplo...00x225.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" />

Suddenly the filter() function has constant runtime of close to 0 seconds—no matter how many elements are in the list. Why is this happening?

The explanation is simple: the filter function returns an iterator, not a list. The iterator doesn’t need to compute a single element until it is requested to compute the next() element. So, the filter() function computes the next element only if it is required to do so. Only if you convert it to a list, it must compute all values. Otherwise, it doesn’t actually compute a single value beforehand.

Related Video: filter() in Python




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!

The post Python filter() first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...on-filter/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Get Milliseconds xSicKxBot 0 41 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 39 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 44 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 48 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 37 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python help() xSicKxBot 0 43 01-22-2021, 01:02 AM
Last Post: xSicKxBot
  [Tut] Python globals() xSicKxBot 0 46 01-19-2021, 04:16 AM
Last Post: xSicKxBot
  [Tut] Python getattr() xSicKxBot 0 54 12-28-2020, 05:11 AM
Last Post: xSicKxBot
  [Tut] Python delattr() xSicKxBot 0 56 12-26-2020, 12:06 AM
Last Post: xSicKxBot
  [Tut] 6 Best Python NLP Libraries xSicKxBot 0 50 12-25-2020, 11:09 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Sick Gaming Welcomes All Gamers
Last Post: Donaldvot
Today 07:17 AM
» Replies: 4
» Views: 10086
(Indie Deal) FREE Press X to Not Die, Yu...
Last Post: xSicKxBot
Today 04:18 AM
» Replies: 0
» Views: 2
Microsoft - Meet 4 women who are using A...
Last Post: xSicKxBot
Today 04:18 AM
» Replies: 0
» Views: 4
News - Gogeta [SS4] Joins The Dragon Bal...
Last Post: xSicKxBot
Today 04:18 AM
» Replies: 0
» Views: 2
News - WandaVision Showrunner Says There...
Last Post: xSicKxBot
Today 04:18 AM
» Replies: 0
» Views: 3
News - Don’t forget to take the GDC 2021...
Last Post: xSicKxBot
Today 02:42 AM
» Replies: 0
» Views: 2
How to get free CS - GO Skins!
Last Post: Augustgeows
Today 01:30 AM
» Replies: 13
» Views: 12625
News - A New Patch For Bloodstained: Rit...
Last Post: xSicKxBot
Yesterday 09:39 PM
» Replies: 0
» Views: 4
News - Best Mario Day Deals: Super Mario...
Last Post: xSicKxBot
Yesterday 09:39 PM
» Replies: 0
» Views: 6
[Tut] How To Run Multiple Python Version...
Last Post: xSicKxBot
Yesterday 03:30 PM
» Replies: 0
» Views: 7

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020