Add lambda as function parameter (#26371)

Added lambda passing as an argument to function,
pull/32824/head^2
Hamza Muric 2019-01-15 23:08:27 +01:00 committed by Manish Giri
parent d3233ec1c4
commit 2e9f341251
1 changed files with 10 additions and 0 deletions

View File

@ -46,6 +46,16 @@ lambda_func(4) # Returns True (4**2 = 16, which is >= 10)
my_dict = {"A": 1, "B": 2, "C": 3}
sorted(my_dict, key=lambda x: my_dict[x]%3) # Returns ['C', 'A', 'B'] # sort dict by the values % 3 (remainders from division by 3)
```
### Passing lambda as fuction parameter
```py
def apply(x, y, fun):
return fun(x, y)
res = apply(3, 5, lambda x, y: x + y)
print(res) # Output: 8
```
### Use-case
Let's say you want to filter out odd numbers from a `list`. You could use a `for` loop: