Add lambda as function parameter (#26371)
Added lambda passing as an argument to function,pull/32824/head^2
parent
d3233ec1c4
commit
2e9f341251
|
@ -46,6 +46,16 @@ lambda_func(4) # Returns True (4**2 = 16, which is >= 10)
|
||||||
my_dict = {"A": 1, "B": 2, "C": 3}
|
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)
|
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
|
### Use-case
|
||||||
|
|
||||||
Let's say you want to filter out odd numbers from a `list`. You could use a `for` loop:
|
Let's say you want to filter out odd numbers from a `list`. You could use a `for` loop:
|
||||||
|
|
Loading…
Reference in New Issue