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
Discussions

Why doesn't Python allow multi-line lambdas? - Software Engineering Stack Exchange
Note that multi-statement lambdas can be done, but only with really, really ugly code. However, what is interesting is how scoping works with this implementation (note the multiple usage of the name variable and the shadowing of the message variable. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
August 7, 2011
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
Simple multi-line lambda syntax - Ideas - Discussions on Python.org
Supports type hints and does not break python semantics afaik app.watch('message', lambda (message: str, origin: str) -> bool: ( # multi-line code here )) More on discuss.python.org
🌐 discuss.python.org
0
June 7, 2023
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
🌐
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
A: No, lambdas can only contain a single expression. You can use a conditional expression (`x if condition else y`), but not an `if`/`else` statement block. Q: Why do I get a `SyntaxError` when I try to write a multiline lambda function Python?
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

🌐
Kodeclik
kodeclik.com › python-multiline-lambda
Can you write a multiline lambda in Python?
September 19, 2024 - This is because the definition of the lambda function has many statements and thus this is a multiline lambda (even though you haven’t split it over multiple lines) and hence disallowed.
🌐
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.rules", "No presence detected for 10 minutes.
🌐
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.
🌐
Flexiple
flexiple.com › python › python-lambda
Python Lambda - Flexiple
March 13, 2024 - ... Through this approach, Python's ... functions are designed for simple operations, but multiple statements can be integrated using techniques like tuples or the and operator....
Find elsewhere
🌐
Real Python
realpython.com › lessons › multiple-expressions-lambda
Multiple Expressions in lambda (Video) – Real Python
It’s going to print whatever h is, which will be "Hello"—and we’ll want it to end with a space (' ') because we want the next word to appear on the same line—or we’re going to print w, "World". 03:23 And so if I call lambda_func(), Python knows that it’s expecting two parameters. The first one I’ll say "Hello", and the second one I’ll say "World". 03:40 And it worked! What happened here? Well, it takes advantage of the fact that the print statement returns None when it needs to have its value determined and that None is interpreted as a False value.
Published: May 18, 2021
🌐
pythontutorials
pythontutorials.net › blog › is-it-possible-to-have-multiple-statements-in-a-python-lambda-expression
Is Multiple Statements in Python Lambda Possible? Solving the Second Smallest Number in Sublists — pythontutorials.net
Test Edge Cases: Validate sublists with duplicates, empty lists, or exactly 2 elements. Python lambda functions cannot handle multiple statements, but they can encapsulate complex logic using a single expression.
🌐
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 - Backslashes \ are used to continue the expression on the next line. The result is the sum of x and y minus the division of x * y by x + y. Another method for creating multiline lambdas is by using explicit return statements.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Explanation: lambda function performs both addition and multiplication and returns a tuple with both results.
Published: May 18, 2026
🌐
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 - Answer a question I am a python newbie trying to achieve the following: I have a list of lists: lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]] I want map lst into another list containing Mangs Python
🌐
sqlpey
sqlpey.com › python › top-10-ways-to-solve-can-python-lambda-expressions-contain-multiple-statements
Top 10 Ways to Solve: Can Python Lambda Expressions Contain Multiple Statements?
November 6, 2024 - A: No, lambda expressions in Python can only contain a single expression, but you can exploit various techniques to simulate the functionality of multiple statements.
🌐
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
🌐
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
🌐
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 ))
🌐
Guru99
guru99.com › home › python › python lambda functions with examples
Python Lambda Functions with EXAMPLES
July 10, 2026 - 3. This is the body of the function, which adds the 2 parameters we passed. Notice that it is a single expression. You cannot write multiple statements in the body of a lambda function.
🌐
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.
🌐
Grokbase
grokbase.com › t › python › python-ideas › 138sy44ytq › multiple-statement-lambda-expressions
[Python-ideas] Multiple statement lambda expressions - Grokbase
August 24, 2013 - What about this? lambda x, y: a = sum(x)/len(x); b = sum(y)/len(y); (a+b)/2;; The double-semicolon notation can also replace indentation for grouping of statements: y=0 for x in list: y=2*y+x if y==0: y;;;; ... Can you give an example where having multiple statements on one line wouldn't ...