Unlock Python’s Power: Master Lambda Anonymous Functions in Minutes
This article introduces Python’s lambda anonymous functions, explains their syntax and differences from regular functions, showcases practical one‑line examples, explores functional programming tools like map, filter, and reduce, and highlights how lambdas can reduce code size while enhancing readability.
Preface
First of all, happy holidays! Learning should continue even during festivals. In a previous article I discussed a three‑line Python interview question.
Let’s learn about anonymous functions (lambda). The name tells you the function has no explicit name.
Anonymous Functions
Anonymous functions consist of a single line of code:
lambda argument1, argument2, ... argumentN : expressionExample:
f = lambda a, b, c: a+b+c
print(f(6, 6, 6))Written as a regular function:
def f(a, b, c):
return a+b+c
print(f(6, 6, 6))The lambda keyword defines the anonymous function. The part before the colon lists the variables, and the part after the colon is the expression whose result is returned automatically, without an explicit return statement.
Difference from Regular Functions
Because a lambda is an expression, it can be used directly inside a list comprehension, unlike a normal function. [(lambda x: x**2)(x) for x in range(10)] Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] The extra (x) calls the lambda with the current loop variable, turning the stored function objects into actual results.
Some Interesting Anonymous Functions
No‑argument lambda:
t = lambda: True
t()Lambda with default argument values:
f2 = lambda x, y=3: x*y
print(f2(2))Expression with inline arguments:
(lambda x, y: x if x>y else y)(103, 102)Benefits of Anonymous Functions
Using lambdas can reduce the amount of code you need to write, which becomes noticeable in larger projects.
Functional Programming
Python supports functional programming, where code is built from pure functions that have no side effects. A pure function returns the same output for the same input without modifying external state.
Impure example (modifies a list):
l = [1,2,3]
def a(l):
for i in range(len(l)):
l[i] += 10
return l
print(a(l))Pure version (creates a new list):
def b(l):
new_l = []
for item in l:
new_l.append(item+10)
return new_l
print(b(l))Common functional tools:
map(function, iterable)
r = map(lambda x: x**2, [1,2,3])
list(r)Result:
[1, 4, 9]filter(function, iterable)
l = [1,2,3,4,5]
new_list = filter(lambda x: x % 2 == 0, l)
list(new_list)Result:
[2, 4]reduce(function, iterable)
from functools import reduce
l = [1,2,3]
r = reduce(lambda x, y: x*y, l)
print(r)Result:
6Conclusion
Although anonymous functions are not frequently used in everyday development, they are essential knowledge for developers and often appear in interview questions. Feel free to contact me via WeChat if you find any errors.
Original content, please like and share if you find it useful.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
