#Learn Python# Python's built-in functions: filter, map, reduce, zip, enumerate

Posted by fredroines on Tue, 04 Jan 2022 08:10:55 +0100


These functions are called high-order functions in Python. This article mainly studies their usage.


The prototype of filter function is as follows:

filter(function or None, iterable) --> filter object

The first parameter is the judgment function (the return result needs to be True or False), and the second is the sequence. The function will successively perform function(item) operations on the iterable sequence, and the return result is the sequence composed of the filtered results.
Simple memory: filter the elements in the sequence to obtain the qualified sequence.

my_list = [1, 2, 3]
my_new_list = filter(lambda x: x > 2, my_list)

The returned result is: & lt; filter object at 0x0000000001DC4F98>, Use the list function to enter the contents of the sequence.


The map function prototype is as follows:

map(func, *iterables) --> map object

After the function runs, a list is generated. The first parameter is the function and the second parameter is one or more sequences;
The following code is a simple test case:

my_list = [-1,2,-3]
my_new_list = map(abs,my_list)

After the above code is run, the result is: & lt; map object at 0x0000000002860390>. Use print(list(my_new_list)) to get the result.

The first parameter of the map function can have multiple parameters. When this happens, the next parameter needs to be multiple sequences.

def fun(x, y):
    return x+y
# The fun function has two parameters, so two sequences are required
my_new_list = map(fun, [1, 2, 3], [4, 4, 4])

The map function solves the following problems:

  1. Using the map function, you do not need to create an empty list;
  2. When calling a function, you don't need parentheses. The map function will automatically call the target function;
  3. The map function automatically matches all elements in the sequence.


The prototype of reduce function is as follows:

reduce(function, sequence[, initial]) -> value

The first parameter is the function, and the second parameter is the sequence, which returns the value after the calculation result. The value of this function is that the rolling calculation is applied to continuous values in the list.
The test code is as follows:

from functools import reduce
my_list = [1, 2, 3]

def add(x, y):
    return x+y

my_new_list = reduce(add, my_list)

The final result is 6. If the third parameter is set to 4, you can run the code to view the results. Finally, the conclusion is that the third parameter represents the initial value, that is, the initial value of the accumulation operation.

my_new_list = reduce(add, my_list,4)

Simple memory: accumulate all elements in the sequence.


The zip function prototype is as follows:

zip(iter1 [,iter2 [...]]) --> zip object

The zip function takes the iteratable object as a parameter, packages the corresponding elements in the object into tuples, and then returns a list composed of these tuples.
If the number of elements of each iterator is different, the length of the returned list is the same as that of the shortest object. The tuple can be decompressed into a list by using the asterisk (*) operator.
The test code is as follows:

my_list1 = [1,2,3]
my_list2 = ["a","b","c"]

Show how to use the * operator:

my_list = [(1, 'a'), (2, 'b'), (3, 'c')]

The output results are as follows:

<zip object at 0x0000000002844788>
[(1, 2, 3), ('a', 'b', 'c')]

Simple memory: the function of zip is to map similar indexes of multiple containers, which can be easily used to construct dictionaries.


The prototype of enumerate function is as follows:

enumerate(iterable, start=0)

Parameter Description:

  • Sequence: a sequence, iterator, or other object that supports iteration;
  • Start: subscript start position.

This function is used to combine a traversable data object into an index sequence, and list data and data subscripts at the same time. It is generally used in the for loop.
The test code is as follows:

weekdays = ['Mon', 'Tus', 'Wen', 'Thir']

The returned result is: & lt; enumerate object at 0x0000000002803AB0>.

Summary of this blog

The functions involved in this paper can be combined with lambda expressions, which can greatly improve the coding efficiency. The best learning material is always the official manual

Topics: Python Back-end