There are many built-in functions in Python for processing iterables (such as list).
Some of them are:
Returns an object of map class which elements are modified according to the function.
The map class can be converted to some other collection with functions like:
Output:
Returns an object of filter class which can be converted to other collection via list(), set() and other functions.
Allows filtering specific elements.
if the function returns true - the element will be present in the returned collection.
If the function returns false - it will not be present.
For example we can filter the list to return only the even numbers:
Output:
Returns a boolean. True
- if all elements meet certain condition and
False
- if one or more elements don't meet the condition.
Let's look at the following example:
Output:
Returns a boolean. True
- if at least one of the elements meets certain condition and
False
- if all of the elements don't meet the condition.
Let's look at the following example:
Output:
The reduce
function is part of the functools
module and is used to accumulate a single value from the list.
It receives three arguments:
Let's look at the following example:
Output:
The following calculations are made until the end value - 20 is reached.