Must-Know Built-in Python Functions Vol-2

Turgutguvenc
4 min readJun 3, 2021

In this article, I will continue to give examples of built-in functions in Python that make your code cleaner, save your time, and help you to prevent trivial lines of code.

If you want to read my first article on this content, please click the link;

4.filter()

The filter function takes two arguments these a function and an iterable. The filter function takes a function that must return logical value (True, False) as its first parameter and applies this function to each element of the iterable, such as a list. If the function is None, the identity function is assumed, that is, all elements of iterable that are false are removed. The filter function returns one filter object, taking only values that result in True.

Syntax: filter(function, iterable)

I write the lambda function below that determines whether x is an even number or an odd number. In this case, the filter function applies this lambda function to the list that the filter object returned, taking only values that result in True. To see the results we put the filter function into the list.

The following example shows how to use filter() within regular function definitions;

The following example shows how to use filter() function within without function. We put the None expression as a first argument; the filter function works as the identity function that is, all elements of iterable that are false are removed.

5.zip()

The zip function creates a tuple by grouping the elements of the iterable object as bundles, respectively, and returns it.

Syntax: zip(*iterable)

Let’s try grouping the elements of the list_a and list_b as bundles respectively, in the traditional and long way.

As you can see in the code above, there are a lot of lines of code while the zip() function makes the same operation within just a one-line code. As you can see in the example below, if any iterable object has more than elements from the other iterable object, the zip function will ignore elements that exceed the shortest length of the iterable object.

The use of the zip() function with dictionaries;

6. all() and any()

any() returns true if any of the items is True while it returns False if the iterable object is empty or all the elements in the iterable are false. It takes a single parameter (iterable

syntax: all(iterable)

https://www.programiz.com/python-programming/methods/built-in/any

all() returns true if all of the items are True (or if the iterable is empty).

It takes a single parameter (iterable).

syntax: all(iterable)

https://www.programiz.com/python-programming/methods/built-in/all

Thanks for reading. Please let me know if you have any feedback and question.

--

--

Turgutguvenc

I am a Data Analyst, and Machine Learning, Big Data enthusiast.