Python 's Power Trio: Map, Filter, Reduce - A Fun Guide for Everyone
Mastering Python's Map, Filter, Reduce, and Lambda Functions: A Beginner-Friendly Guide to Boost Your Coding Skills
Introduction
Welcome to the world of Python, where coding becomes an adventure! Today, we are going to unravel the secrets behind the three powerful functions map
, filter
, reduce
. Whether you are a coding wizard, just dipping your toes into the programming potion or you are just passing by, I hope you enjoy this brief enlightening tour.
Lambda: The Swift Sorcerer
Before we begin on our tour with map
, filter
, and reduce
. I would like to introduce you to lambda
The swift sorcerer, capable of casting spells with a single line of code! Think of lambda
as a quick-witted magician who performs tricks without the need for a grand stage. In a more technical term, lambda
helps us create nameless functions (a function performs specific tasks. A named function is a magician that needs a grand stage which is named using def
in the world of Python).
To help us understand lambda better, let's say we want to multiply the amount in our bank account (N10,000) by 10. We initiate the magic process by saying lambda
>>> lambda account_balance: account_balance * 10
after the magic word lambda
the variables we want to modify follow immediately then a colon :
before the action we want to perform on the variables. There are a variety of actions we can perform on the variables.
To activate this magic we simply call it with values of the variables.
For our example
>>> (lambda account_balance: account_balance * 10)(10000)
>>> 100000
Now we are 10 times richer.
We will see other impressive things we can do with lambda
as we progress.
Map: The Shape-Shifter
Our journey begins with map
, the shape-shifter of Python functions! Let us say we also want to make all of our family members, friends and enemies(why not) 10 times richer as well. With map
and the help of our trusty lambda
spell, we can do that for everyone at once. How cool is that?
map
takes the function(action) and applies to the items i.e map(function, items)
and returns a map
object which we would need to transform back to a list
object
Example
#List of everyone's account balance
>>> account_balances = [10000, 9800, 15000, 2000, 525, 8500, 555]
>>> times_ten = map(lambda acc_bal: acc_bal * 10, account_balances)
>>> print(list(times_ten))
>>> [100000, 98000, 150000, 20000, 5250, 85000, 5550]
So quick and easy. By doing this we have eliminated the infamous for
and while
loops.
Filter: The Gatekeeper
Next in our magical arsenal is the filter
, the gatekeeper of Python! Now let us say you want to start a fundraiser with those who have above N20000 in their account in your group. With filter
and our trusty lambda,
we can get all of them.
>>> acc_bal = [100000, 98000, 150000, 20000, 5250, 85000, 5550]
>>> fund_raisers = filter(lambda bal: bal if bal > 20000 else 0, acc_bal)
>>> print(list(fund_raisers))
>>> [100000, 98000, 150000, 85000]
And there you have them! As we have seen filter
takes a function(action) and the items i.e filter(function, items)
. But there is a catch to this magic. The function(action) must either produce a true or false value. filter
returns a filter
object that we have to also convert to a list
object.
Reduce: The Grand Unifier
Finally, we meet reduce
a maestro that eludes most wizards. How does this magic work? Let's say you want to find the person with the highest account balance. reduce
helps us to that. Here's a pictorial representation.
To use the reduce
we have to first import it from the functools. Like I said earlier it's a maestro that eludes most coding wizards.
>>> from functools import reduce
>>> fund_raisers = [100000, 98000, 150000, 85000]
>>> highest = reduce(lambda x, y: x if x > y else y, fund_raisers)
>>> print(highest)
>>> 150000
In this grand finale lambda
compares x and y to keep the greater one. reduce
supplies the value of x and y from the list of account balances.
Conclusion
So, there you have it! map
, filter
and reduce
are your trusty companions in the Python realm, turning the mundane into the magical. Whether you're a beginner marveling at their simplicity or an advanced wizard harnessing their power, these functions are made easier by lambda
. However other named functions declared with def
can also be used in place of lambda
.
I hope you enjoyed this.
For further adventure kindly visit
Header Image by Freepik