DelphiLearn PythonPythonPython GUIRAD Studio

Learn Powerful Python Map, Filter, And Reduce Functions In A Delphi Windows GUI App

blogbanner3 10

Map, Filter, and Reduce are paradigms of functional programming. They allow us to write simpler, shorter code, without necessarily needing to bother about intricacies like loops and branching.

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Map, Filter, and Reduce in Python GUI apps and gets the output. We will discuss them one by one and understand their use cases.

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1
Open Demo01dproj

1. Map

Map applies a function to all the items in an input_list. The map() function iterates through all items in the given iterable and executes the function we passed as an argument on each of them.

Here is the Pseudocode:

Most of the times we want to pass all the list elements to a function one-by-one and then collect the output. For instance:

Map allows us to implement this in a much simpler and elegant way:

Most of the times we use lambdas with map so we did the same. Instead of a list of inputs we can even have a list of functions! Here is the nice example:

Output in Python GUI:

1 9668190
Implementation of Map Function in Python GUI

 

2. Filter

As suggested by its name, the filter creates a list of elements for which a function returns true. Here is a short and concise example:

The filter resembles a for loop but it is a built-in function and faster. Let’s see the output in our Python GUI:

2 5618646
Implementation of Filter Function in Python GUI

 

3. Reduce

Reduce is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list. For example, if you wanted to compute the product of a list of integers.

The example below is the normal way you might go when doing this task in Python (using a basic for loop):

Note that in Python GUI, to get the response, we need to print the results. Let’s rewrite it with reduce:

Let’s see the output in our Python GUI:

3 3652836
Implementation of Reduce Function in Python GUI

 

4. Conclusion

As mentioned previously; Map, Filter, and Reduce are convenience functions. They are there so we can avoid writing more cumbersome code, but avoid using both them and lambda expressions too much.

Don’t force these tools because “we can”, as it can often lead to illegible code that’s hard to maintain. Use them only when it’s absolutely clear what’s going on as soon as we look at the function or lambda expression.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *