Reduce() seems to be a Python method that performs the folding or compression of the mathematical approach. When you’re about to implement a method over an iterable and limit it to a singular cumulative value, reduce() comes in handy. Python’s reduce() method is famous among functional programmers, although Python has many more to offer. You’ll learn how to use reduce() functions and how to implement them efficiently in this article.

Installation of Python

Before we indulge in the learning of reducing methods in python via the Ubuntu Linux system, we need to have python installed and configured on our Linux system. So to start the learning, we should log in first from the login screen of the Ubuntu system. After the login, launch your command-line shell by activity area or using the shortcut “Ctrl Alt T” key.  You have to check whether your system has python installed already or not. And, for this purpose, try out the query below. You can see it is now showing the latest version. For that, we have to check its version that has already been installed through the below command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-48.png" data-lazy- height="35" src="data:image/svg xml,” width=”475″>

Now, you need to update the apt package that has already been mounted on your Linux system to update python again.  For updation of the apt package, use the stated-below query in the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-49.png" data-lazy- height="68" src="data:image/svg xml,” width=”673″>

After the updation of the apt package, you need to download the additional packages via the ppa package throughout the below query needed for additional python updates.

$ sudo add-apt-repository ppa:deadsnakes/ppa

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-48.png" data-lazy- height="36" src="data:image/svg xml,” width=”694″>

Tap on the Enter key to continue the installation.<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-42.png" data-lazy- height="59" src="data:image/svg xml,” width=”589″>

After downloading additional packages and updates, we will mount the new version of the Python repository in the Ubuntu System. Hence, we have been trying the beneath query in the shell as follows:

$ sudo apt install python3.9

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-47.png" data-lazy- height="71" src="data:image/svg xml,” width=”555″>

Tap “Y” to carry on the procedure of setting up, or else knock out the key “n” to end it. Now the updated version of Python has been sufficiently configured on your system. Now, another package should be installed and configured on your system to use python on it. This package is named “pip.” This is necessary to be installed before the usage of python. As the pip package is not installed, we will be installing it via curl command. Therefore, install the curl package first as:

After the installation of curl, download the package of pip via:

$ curl <a href=“https://bootstrap.pypa.io/get-pip.py”>https://bootstrap.pypa.io/get-pip.py</a> -o get-pip.py

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image7-31.png" data-lazy- height="90" src="data:image/svg xml,” width=”719″>

You can have a look at the list of all the repositories beginning with “get.”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-38.png" data-lazy- height="40" src="data:image/svg xml,” width=”402″>

Using the sudo command with the keyword “python3.9”, we will extract the “py” file below. Please wait for a while until it gets completed.

$ sudo python3.9 get-pip.py

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image9-23.png" data-lazy- height="70" src="data:image/svg xml,” width=”552″>

The last line of the output shows that the latest pip package is installed and mounted on Ubuntu 20.04 System.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image8-25.png" data-lazy- height="86" src="data:image/svg xml,” width=”721″>

You can have a look at the latest installed version of the package “pip” as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image12-11.png" data-lazy- height="42" src="data:image/svg xml,” width=”662″>

Reduce Example 01:

Let’s have a first example of reduce function to see its working. First of all, open the terminal and create a new .py file named “one.py” using the touch query as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image10-17.png" data-lazy- height="17" src="data:image/svg xml,” width=”425″>

Now go to the File Explorer and direct towards the home folder. Open the newly created one.py file and write the script shown below in it. This script contains the function of reducing in it. Firstly, you need to import the reduce method from the library functools to use this function in our code using the “from” keyword. After that, we have declared a method “sum” to calculate a sum of two variables, “a” and “b,” and return this sum to a calling statement. After that, we have initialized a list of integers, and a print statement has been used to apply the reduce function. We have passed the list to a function “sum,” and the “sum” has been passed to the reduce method. You can see we have applied the sum function to the list “l.” So this will sum up the values of the list and return them to the print statement to be shown. Save the file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image11-12.png" data-lazy- height="160" src="data:image/svg xml,” width=”446″>

Let’s execute the saved file by using the “python3” keyword command while using the name of a file “one.py” along with it and tap on the “Enter” button. The terminal’s output shows the sum of all the integer values of a list as 24.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image13-11.png" data-lazy- height="37" src="data:image/svg xml,” width=”450″>

Reduce Example 02:

Let’s take another simple yet lengthy example of reduce method in python to understand it more. Open the same file “one.py” from the home directory and update the code with the below script. Now, this script contains library functools that have been importing the reduce method first. After that, we have defined a function factorial with 2 parameters, x and y, to calculate the factorial of integers provided in some time. The print statement is printing the integer variables and return the calculated factorial or multiplication of both the variables to the calling statement. Then we have defined a 5 element list “l.” The next two print statements have been used to print string values and the sum of the list elements, consequently using the lambda function within the reduce function to avoid complications. The next two print statements are defined to check the largest value from the list using lambda using the reducing method. The last two print statements have been used to print and calculate the factorial by using list members.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image14-7.png" data-lazy- height="322" src="data:image/svg xml,” width=”552″>

We have found the sum, the largest value, and the factorial of the list values in a sequence without any complexity by executing the file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image15-5.png" data-lazy- height="165" src="data:image/svg xml,” width=”465″>

Conclusion:

Hopefully! You will find this article helpful and easy to implement on your side. Now you can easily use python reduce function by implementing different examples of your own choice.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Author-Image-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.