
- 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
How to create a lambda inside a Python loop?
You can create a list of lambdas in a python loop using the following syntax −
Syntax
def square(x): return lambda : x*x listOfLambdas = [square(i) for i in [1,2,3,4,5]] for f in listOfLambdas: print f()
Output
This will give the output −
1 4 9 16 25
You can also achieve this using a functional programming construct called currying.
example
listOfLambdas = [lambda i=i: i*i for i in range(1, 6)] for f in listOfLambdas: print f()
Output
This will give the output −
1 4 9 16 25
- Related Articles
- How to handle exception inside a Python for loop?
- How do I create dynamic variable names inside a JavaScript loop?
- How to create a triangle using Python for loop?
- How to create Tkinter buttons in a Python for loop?
- Can we iteratively import python modules inside a for loop?
- How to convert a Python for loop to while loop?
- How to create a thread using lambda expressions in Java?\n
- How to handle a python exception within a loop?
- How to deploy a Python Docker image to AWS Lambda?
- How to break a for loop in Python?
- How to create multiple styles inside a TextView in Android?
- How to create a rectangle inside boxplot in base R?
- How to create a dynamic 2D array inside a class in C++
- How to put comments inside a Python dictionary?
- How to emulate a do-while loop in Python?

Advertisements