0% found this document useful (0 votes)
2 views

Python Tutorial_ Lambda Operator, filter, reduce and map

This document is a tutorial on Python's lambda operator, filter, map, and reduce functions, detailing their usage and syntax. It discusses the historical context of these features, their advantages, and how they can be applied in programming. The tutorial also emphasizes the importance of list comprehension as an alternative to these functions, particularly in Python 3.

Uploaded by

Sentinel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Tutorial_ Lambda Operator, filter, reduce and map

This document is a tutorial on Python's lambda operator, filter, map, and reduce functions, detailing their usage and syntax. It discusses the historical context of these features, their advantages, and how they can be applied in programming. The tutorial also emphasizes the importance of list comprehension as an alternative to these functions, particularly in Python 3.

Uploaded by

Sentinel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Course

Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact

Previous Chapter: Regular Expressions, Advanced


Next Chapter: List Comprehension

Lambda, filter, reduce and map


Follow Bernd Klein,
the author of this
Lambda Operator website, at Google+:
Bernd Klein on
Python 3 If Guido van Rossum, the author of the programming language Python, had got his will, this chapter would be missing in our tutorial. In his article from May 2005 "All Things
Google
Tutorial Pythonic: The fate of reduce() in Python 3000", he gives his reasons for dropping lambda, map(), filter() and reduce(). He expected resistance from the Lisp and the scheme
Bernd Klein on
"folks". What he didn't anticipate was the rigidity of this opposition. Enough that Guido van Rossum wrote hardly a year later: "After so many attempts to come up with an
Facebook
The Origins of alternative for lambda, perhaps we should admit defeat. I've not had the time to follow the most recent rounds, but I propose that we keep lambda, so as to stop wasting
Python everybody's talent and time on an impossible quest."
Starting with We can see the result: lambda, map() and filter() are still part of core Python. Only reduce() had to go; it moved into the module functools.
Search this website:
Python: The
His reasoning for dropping them is like this:
Interactive Shell
Go
Executing a There is an equally powerful alternative to lambda, filter, map and reduce, i.e. list comprehension
Script List comprehension is more evident and easier to understand This topic in German
Indentation Having both list comprehension and "Filter, map, reduce and lambda" is transgressing the Python motto "There should be one obvious way to solve a problem" / Deutsche
Data Types and Übersetzung:
Variables Lambda, filter, reduce
Operators und map
Some like it, others hate it and many are afraid of the lambda operator. The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are
Sequential Data throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination with the functions filter(), map() and reduce(). The lambda feature was added to
Types: Lists and Python due to the demand from Lisp programmers. Python 3
Strings
List The general syntax of a lambda function is quite simple: This is a tutorial in
Manipulations Python3, but this
chapter of our course
Shallow and lambda argument_list: expression
is available in a
Deep Copy
The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name. version for Python
Dictionaries 2.x as well: Lambda
Sets and Frozen Operator, filter,
The following example of a lambda function returns the sum of its two arguments:
Sets reduce and map in
An Extensive >>> sum = lambda x, y : x + y Python 2.x
Example Using >>> sum(3,4)
7
Sets >>> Classroom
input via the Training
keyboard Courses
Conditional The above example might look like a plaything for a mathematician. A formalism which turns an easy to comprehend issue into an abstract harder to grasp formalism. Above all, we could have had the same effect
Statements by just using the following conventional function definition: The goal of this
Loops, while website is to provide
>>> def sum(x,y): educational material,
Loop ... return x + y
allowing you to learn
For Loops ...
>>> sum(3,4) Python on your own.
Difference Nevertheless, it is
7
between >>> faster and more
interators und efficient to attend a
Iterables We can assure you that the advantages of this approach will be apparent, when you will have learnt to use the map() function. "real" Python course
Output with Print in a classroom, with
Formatted output an experienced
with string The map() Function trainer. So why not
attend one of the live
modulo and the
As we have mentioned earlier, the advantage of the lambda operator can be seen when it is used in combination with the map() function. Python courses in
format method
map() is a function which takes two arguments: Strasbourg, Paris,
Functions Luxembourg,
Recursion and Amsterdam, Zürich /
Recursive r = map(func, seq) Zurich, Vienna /
Functions Wien, London, Berlin,
Parameter Munich, Hamburg,
Passing in The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq. Before Python3, map() used to return a list, Frankfurt or Lake
where each element of the result list was the result of the function func applied on the corresponding element of the list or tuple "seq". With Python 3, map() returns an iterator. Constance by Bernd
Functions
Klein, the author of
Namespaces
The following example illustrates the way of working of map(): this tutorial?
Global and Local
Variables >>> def fahrenheit(T):
Decorators ... return ((float(9)/5)*T + 32)
... Onsite Training
Memoization with >>> def celsius(T): Courses
Decorators ... return (float(5)/9)*(T-32)
Read and Write ... Let us come to your
Files >>> temperatures = (36.5, 37, 37.5, 38, 39)
company,
>>> F = map(fahrenheit, temperatures)
Modular >>> C = map(celsius, F) organization or
Programming >>> institute and train
and Modules >>> temperatures_in_Fahrenheit = list(map(fahrenheit, temperatures)) your employees, as
>>> temperatures_in_Celsius = list(map(celsius, temperatures_in_Fahrenheit)) we've done it many
Packages in
>>> print(temperatures_in_Fahrenheit) times in Amsterdam
Python [97.7, 98.60000000000001, 99.5, 100.4, 102.2] (The Netherlands),
Regular >>> print(temperatures_in_Celsius) Berlin (Germany),
Expressions [36.5, 37.00000000000001, 37.5, 38.00000000000001, 39.0]
Bern (Switzerland),
>>>
Regular Basel (Switzerland),
Expressions, In the example above we haven't used lambda. By using lambda, we wouldn't have had to define and name the functions fahrenheit() and celsius(). You can see this in the following interactive session: Zurich (Switzerland),
Advanced Locarno
Lambda >>> C = [39.2, 36.5, 37.3, 38, 37.8] (Switzerland), Den
Operator, Filter,
>>> F = list(map(lambda x: (float(9)/5)*x + 32, C)) Haag (The Hague),
>>> print(F) Hamburg (Germany),
Reduce and Map [102.56, 97.7, 99.14, 100.4, 100.03999999999999] Frankfurt (Germany),
List >>> C = list(map(lambda x: (float(5)/9)*(x-32), F))
Toronto (Canada),
Comprehension >>> print(C)
[39.2, 36.5, 37.300000000000004, 38.00000000000001, 37.8] Edmonton (Canada),
Iterators and Munich (Germany)
>>>
Generators and many other
Exception map() can be applied to more than one list. The lists don't have to have the same length. map() will apply its lambda function to the elements of the argument lists, i.e. it first applies to the elements with the 0th cities. We do training
Handling index, then to the elements with the 1st index until the n-th index is reached: courses in England,
Tests, DocTests, Switzerland,
>>> a = [1, 2, 3, 4] Liechtenstein,
UnitTests >>> b = [17, 12, 11, 10] Austria, Germany,
Object Oriented >>> c = [-1, -4, 5, 9] France, Belgium, the
Programming >>> list(map(lambda x, y : x+y, a, b))
Netherlands,
Class and [18, 14, 14, 14]
>>> list(map(lambda x, y, z : x+y+z, a, b, c)) Luxembourg, Poland,
Instance [17, 10, 19, 23] UK, Italy and other
Attributes >>> list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c)) locations in Europe
Properties vs. [37.5, 33.0, 24.5, 21.0] and in Canada.
>>>
getters and
setters This way you will get
If one list has less elements than the others, map will stop, when the shortest list has been consumed: a perfect training up
Inheritance
to your needs and it
Multiple >>> a = [1, 2, 3]
>>> b = [17, 12, 11, 10] will be extremely cost
Inheritance efficient as well.
>>> c = [-1, -4, 5, 9]
Magic Methods >>> Contact us so we can
and Operator >>> list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c)) define and find the
Overloading [37.5, 33.0, 24.5] best course
>>> curriculum to meet
OOP, Inheritance
Example your needs, and
schedule course
Slots We can see in the example above that the parameter x gets its values from the list a, while y gets its values from b and z from list c. sessions to be held at
Classes and
your location.
Class Creation
Road to Mapping a List of Functions
Metaclasses Skilled Python
Metaclasses The map function of the previous chapter was used to apply one function to one or more iterables. We will now write a function which applies a bunch of functions, which may be an iterable such as a list or a tuple, Programmers
Metaclass Use for example, to one Python object.
Case: Count You are looking for
from math import sin, cos, tan, pi
Function Calls experienced Python
Abstract Classes def map_functions(x, functions): developers or
""" map an iterable of functions on the the object x """ programmers? We
Usages of the res = [] can help you, please
for func in functions: contact us.
Greek Letter res.append(func(x))
Lambda return res
Quote of the
Both the upper-case family_of_functions = (sin, cos, tan) Day:
print(map_functions(pi, family_of_functions))
letter and the lower
case letter λ are "The digital
The previous program returns the following output: revolution is far more
frequently used in
science and other [1.2246467991473532e-16, -1.0, -1.2246467991473532e-16] significant than the
areas. Though invention of writing
mathematicians The previously defined map_functions function can be simplified with the list comprehension technique, which we will cover in the chapter list comprehension: or even of printing."
prefer the ∅ sign, Λ (Douglas Engelbart)
can denote the def map_functions(x, functions):
empty set. Lambda return [ func(x) for func in functions ]
signifies the set of
logical axioms in the
axiomatic method of
logical deduction in Data Protection
first-order logic. Filtering Declaration
Mathematicians,
those working in The function Data Protection
number theory, found Declaration
another usage for it, filter(function, sequence)
the Mangoldt
function. statisticians offers an elegant way to filter out all the elements of a sequence "sequence", for which the function function returns True. i.e. an item will be produced by the iterator result of filter(function, sequence) if item is
use Λ for Wilks's included in the sequence "sequence" and if function(item) returns True.
lambda in
multivariate analysis In other words: The function filter(f,l) needs a function f as its first argument. f has to return a Boolean value, i.e. either True or False. This function will be applied to every element of the list l. Only if f returns True
of variance to will the element be produced by the iterator, which is the return value of filter(function, sequence).
compare group
means on a In the following example, we filter out first the odd and then the even elements of the sequence of the first 11 Fibonacci numbers:
combination of
dependent variables.
But that's not all, >>> fibonacci = [0,1,1,2,3,5,8,13,21,34,55]
there is still another >>> odd_numbers = list(filter(lambda x: x % 2, fibonacci))
>>> print(odd_numbers)
good usage in
[1, 1, 3, 5, 13, 21, 55]
mathematics: lambda >>> even_numbers = list(filter(lambda x: x % 2 == 0, fibonacci))
indicates the diagonal >>> print(even_numbers)
matrix of the [0, 2, 8, 34]
eigenvalues of a >>>
matrix. >>>
Let's turn to the >>> # or alternatively:
...
Physicists: They use
>>> even_numbers = list(filter(lambda x: x % 2 -1, fibonacci))
the capital lambda to >>> print(even_numbers)
name a type of [0, 2, 8, 34]
subatomic particle in >>>
subatomic particle
physics, i.e. the
lambda particle. In
astrophysics, lambda Reducing a List
denotes the
likelihood that a
As we mentioned in the introduction of this chapter of our tutorial. reduce() had been dropped from the core of Python when migrating to Python 3. Guido van Rossum hates reduce(), as we can learn from his
small body will
statement in a posting, March 10, 2005, in artima.com:
encounter a planet or
a dwarf planet "So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I
leading to a need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much
deflection of a limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly."
significant
magnitude. Λ also The function
represents the period
of a lattice. Lambda reduce(func, seq)
signifies the grating
pitch of a Bragg continually applies the function func() to the sequence seq. It returns a single value.
reflector in optics.
Finally, lambda If seq = [ s1, s2, s3, ... , sn ], calling reduce(func, seq) works like this:
denotes the time
window over which a At first the first two elements of seq will be applied to func, i.e. func(s1,s2) The list on which reduce() works looks now like this: [ func(s1, s2), s3, ... , sn ]
process is observed In the next step func will be applied on the previous result and the third element of the list, i.e. func(func(s1, s2),s3)
for determining the The list looks like this now: [ func(func(s1, s2),s3), ... , sn ]
working memory set Continue like this until just one element is left and return this element as the result of reduce()
for a digital
computer's virtual
memory
management in
computer science.
This has only covered
the capital lambda.
Lover-case λ is even
more frequently
used: It can indicate
the wavelength of
any wave in physics,
If n is equal to 4 the previous explanation can be illustrated like this:
electronics
engineering, and
We want to illustrate this way of working of reduce() with a simple example. We have to import functools to be capable of using reduce:
mathematics. λ
denotes the >>> import functools
radioactivity decay >>> functools.reduce(lambda x,y: x+y, [47,11,42,13])
constant in nuclear 113
physics and >>>
radioactivity. Lower-
case Lambda can also
denote the long-term The following diagram shows the intermediate steps of the calculation:
intrinsic growth rate
of a population.
Lambda is also used
to denote the failure
rate of devices and
systems in reliability
theory.

Mostly, lambda is
used in mathematics
and computer
science: Lambda
denotes a Lagrange
multiplier in multi-
dimensional calculus.
Lambda stands for
the eigenvalue in the
mathematics of linear
algebra. Lambda
denotes the
Lebesgue measure in
mathematical set
theory. Lambda λ is
used to stand for an
empty string in
formal language
theory in computer
science. Lambda
denotes Christopher
Langton's parameter
for the classification
of Stephen Wolfram's
classes of cellular
automata.

What's interesting us
most in our Python
context is its usage
for anonymous
functions. It's both in
mathematical logic
and computer science
used to express the
concepts of the
lambda calculus. In
programming it had
become famous
Examples of reduce()
through the
programming Determining the maximum of a list of numerical values by using reduce:
language Lisp. Lisp
>>> from functools import reduce
programmers had >>> f = lambda a,b: a if (a > b) else b
been responsible for >>> reduce(f, [47,11,42,102,13])
it incorporation into 102
Python. >>>

This website is Calculating the sum of the numbers from 1 to 100:


created by:
>>> from functools import reduce
>>> reduce(lambda x, y: x+y, range(1,101))
5050

It's very simple to change the previous example to calculate the product (the factorial) from 1 to a number, but do not choose 100. We just have to turn the "+" operator into "*":

>>> reduce(lambda x, y: x*y, range(1,49))


12413915592536072670862289047373375038521486354677760000000000

If you are into lottery, here are the chances to win a 6 out of 49 drawing:

>>> reduce(lambda x, y: x*y, range(44,50))/reduce(lambda x, y: x*y, range(1,7))


13983816.0
>>>
Python Training
Courses in Toronto,
Canada
On site trainings in
Europe, Canada and Exercises
the US.
1. Imagine an accounting routine used in a book shop. It works on a list with sublists, which look like this:

Order Number Book Title and Author Quantity Price per Item
34587 Learning Python, Mark Lutz 4 40.95
98762 Programming Python, Mark Lutz 5 56.80
77226 Head First Python, Paul Barry 3 32.95
88112 Einführung in Python3, Bernd Klein 3 24.99

Write a Python program, which returns a list with 2-tuples. Each tuple consists of a the order number and the product of the price per items and the quantity. The product should be increased by 10,- € if the
value of the order is less than 100,00 €.
Write a Python program using lambda and map.

2. The same bookshop, but this time we work on a different list. The sublists of our lists look like this:
[ordernumber, (article number, quantity, price per unit), ... (article number, quantity, price per unit) ]
Write a program which returns a list of two tuples with (order number, total amount of order).

Solutions to the Exercises

1. orders = [ ["34587", "Learning Python, Mark Lutz", 4, 40.95],


["98762", "Programming Python, Mark Lutz", 5, 56.80],
["77226", "Head First Python, Paul Barry", 3,32.95],
["88112", "Einführung in Python3, Bernd Klein", 3, 24.99]]

min_order = 100
invoice_totals = list(map(lambda x: x if x[1] >= min_order else (x[0], x[1] + 10),
map(lambda x: (x[0],x[2] * x[3]), orders)))

print(invoice_totals)

The output of the previous program looks like this:

[('34587', 163.8), ('98762', 284.0), ('77226', 108.85000000000001), ('88112', 84.97)]

2. from functools import reduce

orders = [ [1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)],


[2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
[3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
[4, ("8732", 7, 11.99), ("7733",11,18.99), ("88112", 5, 39.95)] ]

min_order = 100
invoice_totals = list(map(lambda x: [x[0]] + list(map(lambda y: y[1]*y[2], x[1:])), orders))
invoice_totals = list(map(lambda x: [x[0]] + [reduce(lambda a,b: a + b, x[1:])], invoice_totals))
invoice_totals = list(map(lambda x: x if x[1] >= min_order else (x[0], x[1] + 10), invoice_totals))

print (invoice_totals)

We will get the following result:

[[1, 678.3299999999999], [2, 494.46000000000004], [3, 364.79999999999995], [4, 492.57]]

Previous Chapter: Regular Expressions, Advanced


Next Chapter: List Comprehension

© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein

You might also like