This article is participating in Python Theme Month. See the link to the event for more details

An overview of the

Python contains a number of predefined built-in functions that end users can use by simply calling them. These features not only make the programmer’s job easier, but also help establish a common coding environment. In this tutorial, you’ll learn about three of Python’s most powerful functions: map(), filter(), and Reduce ().

The three pillars of functional programming are map, Filter, and Reduce functions. While Python is not entirely a functional programming language, it does have a number of features that make it so. This article discussed the Map, Filter, and Reduce functions in Python and how they correspond to the principles of functional programming.

Functional programming

The programming paradigm of using functions to define computations is called functional programming. The concept of an immutable state is one of the key defining characteristics of functional programming.

Computation is done through statements in imperative programming, arguably the most popular programming paradigm with which you are already familiar. These commands affect the value of the variable and thus the state of the calculation after execution. For example, a for loop can repeat a statement, changing the value of a variable each time, as follows:

counter = 0
for i in range(10):
    counter += 1
Copy the code

During each iteration of the loop, each time the value of the counter is increased by 1, the calculation state changes, bringing it closer to the end state.

Before we discuss the examples of “map()”, “filter()”, and “reduce()” functions in Python, we need to understand another concept: higher-order functions.

Higher-order functions

In functional programming, higher-order functions are our primary tool for defining computations. These are functions that take a function as an argument and return a function as a result. Reduce(), map(), and filter() are three of the most useful higher-order functions in Python. When paired with simpler functions, they can be used to perform complex operations.

The following code example demonstrates a higher-order function. Print greeting() takes two arguments, the function f and the name n, and returns the result of calling f. (n).

def greet(name) :
    return "Hello, {}!".format(name)
Copy the code
def print_greeting(f, n) :
    print(f(n))
Copy the code

Anonymous functions

We saw in the last section an example of a function that takes another function as an argument. The functions map(), filter(), and reduce() all do the same thing: they each take a function and a list of elements, and then return the result of applying the function to each element in the list.

As mentioned earlier, Python has built-in functions such as map(), filter(), and Reduce (). The functional programming features of Python are enabled through these functions. In functional programming, the only thing that determines the output is the input passed in. These functions can take any other function as an argument, and can also be passed as an argument to other functions. Now let’s look at each of these functions in more detail.

We have three main functions:

  1. The map ()
  2. Filter ()
  3. To reduce ()

1. The map () function:

The map() function is a higher-order function. As mentioned earlier, this function takes another function and a series of “iterables” as parameters and provides output after applying the function to each iterable in the sequence. It has the following syntax:

Syntax: mapping (function, iterable)

This function is used to define an expression and then apply it to an “iterable.” Both user-defined functions and lambda functions can be sent to the Map function.

User-defined functions can be sent to the map() method. The user or programmer is the only person who can change the parameters of these functions.

example

def function(a) : 
    return a*a 
x = map(function, (1.2.3.4)) #x is the map object
print(x) 
print(set(x))
Copy the code

The output

{16.1.4.9}
Copy the code

X is a map object, as you can see. Next displays the map function, which takes “function()” and then applies “a * a” to all ‘iterables’. As a result, the values of all iterables are multiplied by themselves before being returned.

Let’s see how we can use lambda functions in the map() method.

Lambda in the map() function:

A function without a name is called a lambda function. These functions are often used as inputs to other functions. Let’s try integrating Lambda functions into the map() function.

example

tup= (5.7.22.97.54.62.77.23.73.61)
newtuple = tuple(map(lambda x: x+3 , tup)) 
print(newtuple)
Copy the code

The output

(8.10.25.100.57.65.80.26.76.64)
Copy the code

2. The filter () function:

The filter() function is used to generate an output list of values that return true when the function is called. It has the following syntax:

Syntax: filters (functions, iterables)

This function, like map(), can take user-defined functions and lambda functions as arguments.

example

def func(x) :If x > =3: 
        return x 
y = filter(func, (1.2.3.4))   
print(y) 
print(list(y))
Copy the code

The output

[3.4]
Copy the code

As you can see, y is the filter object, and the list is the set of true values for the condition (x>=3).

Lambda in the filter() function:

The condition to be checked is defined by the lambda function provided as an argument.

example

y = filter(lambda x: (x>=3), (1.2.3.4) Print (list (y))Copy the code

The output

[3.4]
Copy the code

3. The reduce () function:

As the name implies, the reduce() function applies the provided function to the “iterable” and returns a single value.

Syntax: reduce(function, iterables)

This function specifies which expression should be applied to the “iterable” in this case. You must use the feature tools module to import this feature.

example

from functools import reduce

reduce(lambda a,b: a+b,[23.21.45.98])
Copy the code

The output

187
Copy the code

The Reduce function in the previous example adds each iterable in the list and returns a single result.

The Python functions map(), filter(), and reduce() can all be used together.

๐Ÿบ Quick summary — Python’s powerful functions: map(), filter(), and reduce()

So, this article about map(), filter(), and reduce functions in Python. I hope you have understood everything clearly. Make sure you practice on map(), filter(), and reduce(), because this function is very useful when we use Python for data analysis.

If you enjoyed this article and are interested in seeing more of it, you can check out the source code for all my originals and works here:

Making, Gitee

More on this at ๐Ÿงต

  • Python keywords, identifiers, and variables | Python theme month
  • Python statements, expressions, and indentation | Python theme month
  • How to write comments and multi-line comments in Python | Python Theme Month
  • Python data types — Basic to advanced learning | Python topic month
  • 100 Basic Python Interview Questions Part 1 (1-20) | Python topic month
  • 100 Basic Python Interview Questions Part 2 (21-40) | Python topic month
  • 100 Basic Python Interview Questions Part 3 (41-60) | Python topic month
  • 100 Basic Python Interview Questions Part 4 (61-80) | Python topic month
  • 100 Basic Python Interview Questions Part 5 (81-100) | Python topic month
  • 30 Python tutorials and tips | Python theme month

Recommended articles of the past:

  • Teach you to use Java to make a gobang game
  • Interesting way to talk about the history of JavaScript โŒ›
  • The output of a Java program | Java exercises ใ€‘ ใ€ 7 sets (including parsing)
  • โค๏ธ5 VS Code extensions that make refactoring easy โค๏ธ
  • It is said that if you have light in your eyes, everywhere you go is light
  • 140000 | 400 multichannel JavaScript ๐ŸŽ“ interview questions with answers ๐ŸŒ  items (the fifth part 371-424)

If you do learn something new from this post, like it, bookmark it and share it with your friends. ๐Ÿค— Finally, don’t forget โค or ๐Ÿ“‘ for support