- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Why can’t Python lambda expressions contain statements?
Yes, Python Lambda Expressions cannot contain statements. Before deep diving the reason, let us understand what is a Lambda, its expressions, and statements.
The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the syntax −
lambda arguments: expressions
The keyword lambda defines a lambda function. A lambda expression contains one or more arguments, but it can have only one expression.
Let us see an example −
myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr)
Sort a List by values from another list using Lambda
In this example, we will sort a list by values from another list i.e. the 2nd list will have the index in the order in which they are placed in sorted order −
# Two Lists list1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] list2 = [2, 5, 1, 4, 3] print("List1 = \n",list1) print("List2 (indexes) = \n",list2) # Sorting the List1 based on List2 res = [val for (_, val) in sorted(zip(list2, list1), key=lambda x: x)] print("\nSorted List = ",res)
List1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] List2 (indexes) = [2, 5, 1, 4, 3] Sorted List = ['Audi', 'BMW', 'Hyundai', 'Tesla', 'Toyota']
Lambda Expressions cannot contain statements
We have seen two examples above wherein we have used Lambda expressions. Python lambda expressions cannot contain statements because Python’s syntactic framework can’t handle statements nested inside expressions.
Functions are already first-class objects in Python, and can be declared in a local scope. Therefore, the only advantage of using a lambda instead of a locally defined function is that you don’t need to invent a name for the function i.e. anonymous, but that’s just a local variable to which the function object is assigned!
- Related Articles
- Why we use lambda expressions in Java?
- Why milk doesn't contain Vitamin C?
- Lambda Expressions in C#
- How can we use lambda expressions with functional interfaces in Java?
- why Python can't define tuples in a function?
- Generalized Lambda Expressions in C++14
- What are lambda expressions in C#?
- What are lambda expressions in Java?
- Are lambda expressions objects in Java?
- Why plants can't talk?
- How to debug lambda expressions in Java?
- What are block lambda expressions in Java?
- Why humans can't digest cellulose?
- Differences between Lambda Expressions and Closures in Java?
- Why can't we live on Mars?