There are several different answers I can give here, from your specific question to more general concerns. So from most specific to most general:

Q. Can you put multiple statements in a lambda?

A. No. But you don't actually need to use a lambda. You can put the statements in a def instead. i.e.:

def second_lowest(l):
    l.sort()
    return l[1]

map(second_lowest, lst)

Q. Can you get the second lowest item from a lambda by sorting the list?

A. Yes. As alex's answer points out, sorted() is a version of sort that creates a new list, rather than sorting in-place, and can be chained. Note that this is probably what you should be using - it's bad practice for your map to have side effects on the original list.

Q. How should I get the second lowest item from each list in a sequence of lists?

A. sorted(l)[1] is not actually the best way for this. It has O(N log(N)) complexity, while an O(n) solution exists. This can be found in the heapq module.

>>> import  heapq
>>> l = [5,2,6,8,3,5]
>>> heapq.nsmallest(l, 2)
[2, 3]

So just use:

map(lambda x: heapq.nsmallest(x,2)[1],  list_of_lists)

It's also usually considered clearer to use a list comprehension, which avoids the lambda altogether:

[heapq.nsmallest(x,2)[1] for x in list_of_lists]
Answer from Brian on Stack Overflow
Top answer
1 of 5
53

Guido van van Rossum answered it himself:

But such solutions often lack "Pythonicity" -- that elusive trait of a good Python feature. It's impossible to express Pythonicity as a hard constraint. Even the Zen of Python doesn't translate into a simple test of Pythonicity...

In the example above, it's easy to find the Achilles heel of the proposed solution: the double colon, while indeed syntactically unambiguous (one of the "puzzle constraints"), is completely arbitrary and doesn't resemble anything else in Python...

But I'm rejecting that too, because in the end (and this is where I admit to unintentionally misleading the submitter) I find any solution unacceptable that embeds an indentation-based block in the middle of an expression. Since I find alternative syntax for statement grouping (e.g. braces or begin/end keywords) equally unacceptable, this pretty much makes a multi-line lambda an unsolvable puzzle.

http://www.artima.com/weblogs/viewpost.jsp?thread=147358

Basically, he says that although a solution is possible, it's not congruent with how Python is.

2 of 5
30

it's perfectly fine to do a multi line lambda in python: see

>>> f = lambda x: (
...   x**x)
>>> f
<function <lambda> at 0x7f95d8f85488>
>>> f(3)
27

the real lambda limitation is the fact that lambda must be a single expression; it can't contains keyword (like python2's print or return).

GvR choose to do so to limit the size of the lambda, as they normally are used as parameters. If you want a real function, use def

Discussions

Lambda and multiple statements in Python - Stack Overflow
Trying with: Python3+(3.4, 3.2) OS: Linux (Ubuntu), OSX ... I don't want to use lambda in that way, first primer was found in code of nagios plugin, and I'm was curios about why it works in that way:) ... lambdas are expressions and their body must be an expression. You cannot put a statement inside a lambda, let alone multiple ... More on stackoverflow.com
🌐 stackoverflow.com
Can I have a python lambda with more than one statement?
I’m converting some rules using timers today and have a question I can’t find an answer to. With the rules dsl I could define a timer like this; away_timer = createTimer(now.plusMinutes(10)) [| logInfo("presence.rules", "No presence detected for 10 minutes. More on community.openhab.org
🌐 community.openhab.org
1
0
November 18, 2020
One Lambda function that does many things or One thing per lambda
One thing per lambda. You can use lambda layers for shared code between functions. More on reddit.com
🌐 r/aws
16
12
January 31, 2022
multi line lambdas, as god intended
Mfs will do anything not to define a function More on reddit.com
🌐 r/Python
31
53
December 19, 2022
🌐
Python.org
discuss.python.org › ideas
Simple multi-line lambda syntax - Ideas - Discussions on Python.org
June 7, 2023 - Supports type hints and does not break python semantics afaik app.watch('message', lambda (message: str, origin: str) -> bool: ( # multi-line code here ))
🌐
Verve AI
vervecopilot.com › interview-questions › what-are-the-hidden-rules-of-a-multiline-lambda-function-python-in-technical-interviews
What Are The Hidden Rules Of A Multiline Lambda Function Python In… · Function Python In Technical Interv… · Interview Q&A | Verve AI
It cannot contain multiple statements, assignments, `if`/`else` blocks as statements, or explicit `return` statements [^2]. This is where the confusion about a multiline lambda function Python often begins.
🌐
Kodeclik
kodeclik.com › python-multiline-lambda
Can you write a multiline lambda in Python?
September 19, 2024 - Lambda functions are expected to be short and sweet and just contain one expression in their definition.You can split a single line lambda over multiple lines but you technically cannot write a lambda function that is more than a single expression.
🌐
Wordpress
programmingideaswithjake.wordpress.com › 2016 › 10 › 01 › multi-line-lambdas-in-python
Multi-Line Lambdas in Python | Programming Ideas With Jake
January 12, 2018 - Python has one missing feature that actually hurts my idea that Kotlin is a lot like a statically-typed Python: lack of multi-line lambdas; lambdas in Python, as you probably know, are limited to a single expression or statement.
🌐
LabEx
labex.io › tutorials › python-how-to-create-lambda-with-multiple-arguments-420053
How to create lambda with multiple arguments | LabEx
Lambda functions in Python can accept multiple arguments, providing flexibility in creating compact, inline functions. ... ## Using *args for variable arguments sum_all = lambda *args: sum(args) print(sum_all(1, 2, 3, 4, 5)) ## Output: 15
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python lambda with multiple arguments
Python Lambda with Multiple Arguments - Spark By {Examples}
May 31, 2024 - Python lambda can be used with multiple arguments and these arguments are used in evaluating an expression to return a single value. A Python lambda
🌐
Real Python
realpython.com › lessons › multiple-expressions-lambda
Multiple Expressions in lambda (Video) – Real Python
So, it’s a different way to define ... in Python. 01:47 But here is the cool thing that you can do. You can write a lambda which operates on more than one variable....
Published: May 18, 2021
🌐
Afternerd
afternerd.com › blog › python-lambdas
Python Lambdas Explained (With Examples) - Afternerd
April 6, 2019 - Python lambda functions accept only one and only one expression. If your function has multiple expressions/statements, you are better off defining a function the traditional way instead of using lambdas.
🌐
openHAB Community
community.openhab.org › setup, configuration and use › scripts & rules
Can I have a python lambda with more than one statement? - Scripts & Rules - openHAB Community
November 18, 2020 - I’m converting some rules using timers today and have a question I can’t find an answer to. With the rules dsl I could define a timer like this; away_timer = createTimer(now.plusMinutes(10)) [| logInfo("presence.ru…
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "python lambda with multiple lines"
Python Lambda With Multiple Lines | Delft Stack
February 12, 2024 - Another method for creating multiline lambdas is by using explicit return statements. While lambdas usually implicitly return the result of the expression, using the return keyword allows you to have multiple lines in the lambda body.
🌐
Verve AI
vervecopilot.com › interview-questions › why-multiline-lambda-python-isn-t-what-you-think-it-is-for-interview-success
Why Multiline Lambda Python Isn't What You Think It Is For Interview Success · It Is For Interview Success · Interview Q&A | Verve AI
This distinction is crucial: you cannot include multiple statements or complex control flow (like `if`/`else` blocks, `for` loops, or variable assignments) directly within a lambda body.
🌐
CSDN
devpress.csdn.net › python › 62fd2c36c677032930802e61.html
Is it possible to have multiple statements in a python lambda expression?_python_Mangs-Python
August 18, 2022 - It's also usually considered clearer to use a list comprehension, which avoids the lambda altogether:
🌐
Guru99
guru99.com › home › python › python lambda functions with examples
Python Lambda Functions with EXAMPLES
July 10, 2026 - 🔑 Lambda keyword: A lambda is ... holds exactly one expression whose value is returned automatically, so statements and multiple lines are not allowed....
🌐
Mimo
mimo.org › glossary › python › lambda-function
Python Lambda Function: Syntax, Usage, and Examples
For loops allow applying a lambda function to multiple values sequentially. Python supports functional programming, where functions can be passed as arguments or returned from other functions.
🌐
Python
wiki.python.org › moin › MultiLineLambda
MultiLineLambda - Python Wiki
February 6, 2024 - Python provides many other functional constructs (e.g. list comprehensions, match) and the standard library provides many functions that support a functional paradigm (e.g. filter, map). However, there is no support for complex (multi-statement) anonymous functions.
🌐
Dataquest
dataquest.io › home › blog › how to use a lambda function in python
Lambda Functions in Python (With Examples)
April 21, 2026 - Python borrowed the term to describe small, anonymous functions. No. Lambda functions are limited to a single expression. If you need multiple lines of logic, conditionals, or statements like try/except, use a regular function with def.
🌐
Grokbase
grokbase.com › t › python › python-ideas › 138sy44ytq › multiple-statement-lambda-expressions
[Python-ideas] Multiple statement lambda expressions - Grokbase
August 24, 2013 - No-statement ones? If it were required, this would (in my opinion, unnecessarily) break old code. And if it didn't require it, wouldn't both of the following lines be valid: lambda x: x - 1; x + 3; lambda x: x - 1; x + 3;; which could be tricky to debug if one accidentally included/excluded an extra semi-colon.