

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to pass Python function as a function argument?
Python implements the following method where the first parameter is a function −
map(function, iterable, ...) - Apply function to every item of iterable and return a list of the results.
We can also write custom functions where we can pass a function as an argument.
We rewrite given code to pass the function sqr(x) as function argument using map method.
Example
s = [1, 3, 5, 7, 9] def sqr(x): return x ** 2 print(map(sqr, s)) We can as well use lambda function to get same output s = [1, 3, 5, 7, 9] print(map((lambda x: x**2), s))
Output
C:/Users/TutorialsPoint1/~.py [1, 9, 25, 49, 81]
- Related Questions & Answers
- How to pass a dictionary as argument in Python function?
- Java Program to Pass ArrayList as the function argument
- How to pass entire array as an argument to a function in C language?
- How to pass entire structure as an argument to function in C language?
- How to pass an entire structure as an argument to function in C?
- How to pass the address of structure as an argument to function in C?
- How to pass individual elements in an array as argument to function in C language?
- How to pass the address of structure as an argument to function in C language?
- How to pass a json object as a parameter to a python function?
- How to pass a function as a parameter in Java
- How to pass arrays as function arguments in JavaScript?
- How to pass an object as a parameter in JavaScript function?
- Java Program to pass lambda expression as a method argument
- How to call a function with argument list in Python?
- How to pass optional parameters to a function in Python?
Advertisements