
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements