AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python Filter () Function: Filter iterables by conditions

PARTAGEZ

With the function filter() From Python, you filter an iterable according to a specific condition and thus create a new iterator which contains only the verified values. It also works with character strings or to delete zero values.

What is Python filter() ?

With the Python function filter()you can apply a condition for an iterable And only select the elements that satisfy this condition. These elements are then returned as an iterator. The condition is considered fulfilled if it is not « 0 » or « false ». Although there are other options to filter a list or a dictionary, Python filter() can be used in a particularly effective way. The function creates only an object which refers to the starting iterable and provides only the function provided and an index. It is thus much less bulky than other options.

Python Filter() Function: Syntax and parameters

Python syntax filter() presents itself as follows:

filter(function, iterable)

python

Python filter() So have two parameters:

  • function : This argument indicates a function that will be applied to each element within the iterable. It returns a Boolean value (« True » or « False »).
  • iterable : this parameter indicates the sequence which must be filtered by the Python function filter(). For example, it can be a list, a set, another iterable or a python.

The function therefore checks that each iterable element fulfills the condition. If it is « true », the element is taken into account for the new sequence. If the element does not meet the condition, it is no longer included in the list. We show you the exact operation in the following paragraphs using some practical examples.

Example for python filter()

In our first example, we use a series of numbers and we want to ask the system to take into account only the values ​​greater than 10. For that, we use Python filter(). The corresponding code presents itself as follows:

values = [3, 7, 9, 11, 17, 24, 25, 38, 40, 42]
def function(x):
    return x > 10
numbers = filter(function, values)
list_numbers = list(numbers)
print(list_numbers)

python

We then get this new list:

[11, 17, 24, 25, 38, 40, 42]

python

Python function filter() with character strings

Of course, filter() also works with python character strings (thongs in English). In the following example, we tell the system to take into account only vowels. Here is the corresponding code:

values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
def vowels_filter(letter):
    vowels = ['a', 'e', 'i', 'o', 'u']
    if letter in vowels:
        return True
    else:
        return False
vowels_filtered = filter(vowels_filter, values)
vowels = tuple(vowels_filtered)
print(vowels)

python

We converted the exit into a tuple. She looks like this:

Combination with Lambda

It is possible to combine the Python function filter() with an average function; This is a anonymous function which is mainly used locally. In our example, we choose a sequence of numbers and ask the system to take into account only the peer numbers. Here is the code:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_iterator = filter(lambda x: (x % 2 == 0), numbers)
even_numbers = list(new_iterator)
print(even_numbers)

python

We then obtain the following output:

The function filter() with None

If you pass None as a first argument to filter() From Python, the system receives the instruction to filter only « false » values. These are values ​​which, according to the Boolean principle, are neither true (true) nor false (false), but which have on the contrary a zero value ; They are therefore in principle empty. For our example, we choose a list of different values. These are whole, empty or Boolean values. We then use Python filter() with None And sort the values ​​that do not meet our condition. Here is the appropriate code:

values = [1, 2, 3, 'x', 0, 4, True, False, 5]
new_iterator = filter(None, values)
new_values = list(new_iterator)
print(news_values)

python

The exit is then as follows:

[1, 2, 3, 'x', 4, True, 5]

python

Remove empty channels with Python filter()

If you also want to delete empty channels, the easiest way is to use Python filter(). The corresponding command could for example look like this:

sentence = ['This', ' ', 'is', ' ', 'an', ' ', 'example', ' ']
sentence = list(filter(None, sentence))
print(sentence)

python

For the output, the empty channels are deleted and only the chains with a value are taken into account. It looks like this:

"[This, 'is', 'an', 'example']"

python

Advice

Automatic framework detection, simulation environment and many other functions: with Deploy Now of Ionos, the results are automatically deployed on a reliable infrastructure. Choose the price that best suits your needs!

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact