Lambda in Python - Doubt

3

In Python 3 study, what would be the lambda function in Python, what is it for and when is it used?

    
asked by anonymous 10.12.2018 / 11:48

2 answers

1

Lambda are one-line functions (usually one-time calls). They are known as anonymous functions in some languages. You may want to use lambdas when you do not need to use a function more than once in a program. They function as a regular function.

Syntax:

lambda arguments: manipulate(arguments)

Example:

# Definindo uma função regular 
deff add(x , y):
    return x+y

# A mesma função com lambda
add = lambda x, y: x+y  

To call it, use the same syntax for calling the regular function, for example:

print(add(3, 7))
# output: 10

In the above example, the function was created and assigned to a variable, in which case it could be used more than once, but most of the time lambdas is executed inline, discarded after use, as in the examples of map() and filter() , then.

Lambda functions are generally used in python as an argument for "higher order" functions (functions that receive other functions as part of the arguments), often used as inner functions of filter() and map() .

Example using filter()

The function filter() in python receives in its arguments a function and a list and returns a new list containing the items that the function evaluates as True .

mylist = mylist = [1, 2, 3, 4, 5, 6]
newlist = list(filter(lambda x: (x%2 == 0) , mylist))

print(newlist)
[2, 4, 6]

Example using map()

The function map receives a list and a function and returns a new list in which the elements are the result of the application of the function sent in each element of the list received.

mylist = [1, 2, 3, 4, 5, 6]
newlist = list(map(lambda x: x*2, mylist))

print(newlist)
[2, 4, 6, 8, 10, 12]

List Comprehensions

Whenever you feel the need to use lambda where the return is a list, see if you can not use Lists comprehensions , are much more elegant, intuitive and pythonic, see the same examples with them:

# List comprehension like a filter()
mylist = [1, 2, 3, 4, 5, 6]
newlist1 = [n for n in mylist if (n%2==0)]
print(newlist1)
[2, 4, 6]

# List comprehension like a map()
newlist2 = [n*2 for n in mylist1]
print(newlist2)
[2, 4, 6, 8, 10, 12]
    
10.12.2018 / 12:49
0

Lambda is a function defined in the runtime of the program. It can be used to simplify some operations, but more than this lambdas bring a functional semantics to the language, in which you work with objects and functions, an example of using the lambda would take a vector of numbers and multiply them by 2.

With lambda you do not need to explicitly loop:

items = [1, 2, 3, 4, 5]
squared = []

for i in items:
    squared.append(i**2)

print ("squared =", squared)
vc pode usar a função map

def square(x):
    return x**2
items = [1, 2, 3, 4, 5]
squared = list(map(square, items))
print ("squared =", squared)
ou vc pode criar um lambda diretamente

items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
print ("squared =", squared)
    
10.12.2018 / 11:48