Python filter() function

I’ve recently learned that Python has built-in global functions like JavaScript.
Today we’ll be looking into the filter() function.

In general, filters are used to filter a sequence set, for instance, a list.

Filter() function in Python

Let…


This content originally appeared on DEV Community and was authored by Chris Bongers

I've recently learned that Python has built-in global functions like JavaScript.
Today we'll be looking into the filter() function.

In general, filters are used to filter a sequence set, for instance, a list.

Filter() function in Python

Let's first have a look at the syntax:

result = filter(myFunction, input)

To give more details to this:

  • result: Is the output. This will be a filtered sequence. So basically the original input, but without some items
  • filter: Is the Python built-in function
  • myFunction: This will be a custom function we are going to build
  • input: This is the original sequence we want to filter

We'll make a list with numbers. Let's say we want to return only the numbers higher than 10.

input = [2, 11, 3, 23, 105, 1, 9, 10]

def myFunction(n):
    return n > 10

result = filter(myFunction, input)
print(list(result))
# [11, 23, 105]

As you can see, our input array includes different numbers. We create a myFunction that serves as the filter function.
There we say return if the number is bigger than 10 include that number.

Then we call the filter on our input and print out our new list returning in:

[11, 23, 105]

Already superb, but we can even use lambda functions to make it easier!

input = [2, 11, 3, 23, 105, 1, 9, 10]

result = filter(lambda n: n > 10, input)
print(list(result))

And this will result in the same result.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter


This content originally appeared on DEV Community and was authored by Chris Bongers


Print Share Comment Cite Upload Translate Updates
APA

Chris Bongers | Sciencx (2021-06-04T07:46:15+00:00) Python filter() function. Retrieved from https://www.scien.cx/2021/06/04/python-filter-function/

MLA
" » Python filter() function." Chris Bongers | Sciencx - Friday June 4, 2021, https://www.scien.cx/2021/06/04/python-filter-function/
HARVARD
Chris Bongers | Sciencx Friday June 4, 2021 » Python filter() function., viewed ,<https://www.scien.cx/2021/06/04/python-filter-function/>
VANCOUVER
Chris Bongers | Sciencx - » Python filter() function. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/04/python-filter-function/
CHICAGO
" » Python filter() function." Chris Bongers | Sciencx - Accessed . https://www.scien.cx/2021/06/04/python-filter-function/
IEEE
" » Python filter() function." Chris Bongers | Sciencx [Online]. Available: https://www.scien.cx/2021/06/04/python-filter-function/. [Accessed: ]
rf:citation
» Python filter() function | Chris Bongers | Sciencx | https://www.scien.cx/2021/06/04/python-filter-function/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.