## Learn the three functions of map, reduce and filter to make your Python code look more impressive!

Foreword: Everyone thinks Python is easy to learn, but sometimes the code I write is simply ravaged compared to others. When I see the code written by others, I think it is very beautiful, so I summarize a few commonly used functions that can provide your program with style.

### map(func, *iterables)

Process each element in the passed in iterable, returning a new iterator

``````map(func, *iterables)
Function: Put each element in the incoming iterable data into the function for processing, and return a new iterator
parameter:
func function custom function | built-in function
iterables: iterable data
Return value: iterator
``````
• 1
• 2
• 3
• 4
• 5
• 6

Example:

``````# (1) Convert a list of string numbers to a list of integer numbers
# ['1','2','3','4'] # ==> [1,2,3,4]
# Normal processing
varlist =  [ '1' , '2' , '3' , '4' ]   # ==> [1,2,3,4]
newlist =  [ ]
for i in varlist :
newlist . append ( int ( i ) )
print ( newlist )

# Process with map function
varlist =  [ '1' , '2' , '3' , '4' ]
res =  map ( int , varlist )  # <map object at 0x104ea8890>
print ( list ( res ) )

# (2) [1,2,3,4] ==> [1,4,9,16]

# Normal method
varlist =  [ 1 , 2 , 3 , 4 ]
newlist =  [ ]
for i in varlist :
res = i **  2
newlist . append ( res )
print ( newlist )

# Process this data using the map function
varlist =  [ 1 , 2 , 3 , 4 ]
def  myfunc ( x ) :
return x **  2
res =  map ( myfunc , varlist )
print ( res , list ( res ) )

# optimized version of lamda function
res =  map ( lambda x : x ** 2 , varlist )
print ( res , list ( res ) )
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34

### reduce(func, iterable)

``````reduce(func, iterable)
Features:
Each time, two elements are taken from the iterable and put into the func function for processing, and a calculation result is obtained.
Then put this calculation result and the third element in the iterable into the func function to continue the operation,
The obtained result and the fourth element after it are added to the func function for processing, and so on, until the last element participates in the operation
parameter:
func: built-in function or custom function
iterable: iterable data
Return value: the final operation processing result
Note: When using the reduce function, you need to import from functools import reduce
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10

Example:

``````from functools import  reduce

### (1) [5,2,1,1] ==> 5211

# Normal method
varlist =  [ 5 , 2 , 1 , 1 ]
res =  ''
for i in varlist :
res +=  str ( i )
res =  int ( res )

'''
5 2 1 1
5 * 10 + 2 == 52
52 * 10 + 1 == 521
521 * 10 + 1 == 5211
'''

# use reduce to complete
def  myfunc ( x , y ) :
return x * 10 + y
varlist =  [ 5 , 2 , 1 , 1 ]
# call the function
res =  reduce ( myfunc , varlist )
print ( res , type ( res ) )

# (2) Set '456' of the string ==> 456
# How to solve the above problem when it is required that the int method cannot be used for type conversion

# define a function that, given a string of numbers, returns an integer number
def  myfunc ( s ) :
vardict =  { '0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 , '6' : 6 , '7' : 7 , '8' : 8 , '9' :9 }
return vardict [ s ]

# 1. First use the map function to convert the number string to an integer number
iter1 =  map ( myfunc , '456' )

# 2. Use lambda for secondary processing of the values ​​in the list of numbers
iter2 =  reduce ( lambda x , y : x * 10 + y , iter1 )
print ( iter2 )
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40

### filter(func, iterable)

``````filter(func, iterable)
Function: filter data, get each element in iterable to func function for processing,
If the function returns True, keep the data, return False to discard the data
parameter:
func custom function
itereble: iterable data
Return value: iterator of retained data
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7

Example:

``````# Ask to keep all even numbers, discard all odd numbers
varlist =  [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]

# Ordinary method implementation
newlist =  [ ]
for i in varlist :
if i %  2  ==  0 :
newlist . append ( i )
print ( newlist )

# use filter for processing

# Define a function to determine whether the current function is even, return True for even numbers, and False for odd numbers
def  myfunc ( n ) :
if n %  2  ==  0 :
return  True
else :
return  False
#
# # Call the filter function to process
it =  filter ( myfunc , varlist )
print ( it , list ( it ) )

# optimized version
it =  filter ( lambda n : True  if n %  2  ==  0  else  False , varlist )
print ( it , list ( it ) )
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26