# Python. Implementing complex logic in lambda expressions

## Implementing complex logic in lambda expressions. Condition check. Using lambda expressions for sequences. Functions map(), filter(), reduce()

### Contents

Search other resources:

##### 1. How do I provide a condition check in a lambda expression? Example

As you know, in a lambda expression it is impossible to use the if statement in its normal form. However, a lambda expression can use a conditional operator, which has the following general form

`result1 if condition else result2`

here

• condition – conditional expression;
• result1 – the result that is returned if condition = true;
• result2 – the result that is returned if condition = false.

Example. The code for lambda expressions that return the maximum and minimum values between two input objects is demonstrated.

```# Checking a condition in a lambda expression

# Calculating the maximum value between two numbers
maximum = (lambda a, b: a if a>b else b)
print(maximum(15, 13))

# Minimum value between three numbers a, b, c
min = (lambda a, b, c: a if (a<=b)and(b<=c) else (b if (b<=a)and(b<=c) else c))
print(min(9,8,5))```

The result of the program

```15
5```

### ⇑

##### 2. Applying lambda expressions to sequences. Function map(). Examples

If you need to cycle through a sequence (list, tuple) inside a lambda expression, then it makes sense to use the map() function

`map(func, *iterables)`

here

• func – function to be applied to each element of the iterables sequence;
• iterables – an iterated object that is a sequence (for example list).

The function returns a list containing the results of all the function func() calls.

Example 1. Use the map() function to receive a new list in which each element is doubled. Demonstrate how the function works for two cases:

• without using a lambda expression;
• using lambda expression.

```# Using lambda expressions to process sequences

# Task. Multiply each element of the sequence by 2.
# Use lambda expression and function map()

# 1. Declare a function that multiplies a number by 2
def Mult2(t):
return t*2

# 2. The list under test.
L = [ 2, 8, 12, -5, -10 ]

# 3. Apply map() function without lambda expression based on function Mult2()
L2 = list(map(Mult2, L))
print("L2 = ", L2)

# 4. Apply map() function with lambda expression
L3 = list(map((lambda t: t*2), L))
print("L3 = ", L3)```

The result of the program

```L2 = [4, 16, 24, -10, -20]
L3 = [4, 16, 24, -10, -20]```

Example 2. The example demonstrates applying the map() function and a lambda expression to a tuple. Each element of the tuple is converted to an integer.

```# Using lambda expressions to process sequences

# Using a lambda expression and map() function to a tuple

# 1. Declare a test tuple
T = ( 2.88, -1.75, 100.55 )

# 2. Get a new tuple in which the elements of the sequence are cast
# to an integer type
T2 = tuple(map((lambda x: int(x)), T))

# 3. Print the result
print("T2 = ", T2) # T2 = (2, -1, 100)```

The result of the program

`T2 = (2, -1, 100)`

### ⇑

##### 3. Receiving data in sequence. The filter() function. Examples

The filter() function is used to fetch data from a sequence. General form of function

`filter(func or None, iterable)`

here

• func – the name of the function to apply to the iterable object;
• iterable – an iterated object (list, tuple).

The function returns the filtered object.

Example 1. For a given tuple of strings, a new tuple is formed that contains strings of 3 characters long.

```# Using lambda expressions to process a sequence

# Using a lambda expression and the filter() function on a tuple

# 1. Declare a test tuple
T = ( 'abcd', 'abc', 'cdefg', 'def', 'ghi' )

# 2. Get a new tuple that implements strings of length 3 characters
T2 = tuple(filter((lambda s: len(s)==3), T))

# 3. Print the result
print("T2 = ", T2) # T2 = ('abc', 'def', 'ghi')```

The result of the program

`T2 = ('abc', 'def', 'ghi')`

Example 2. For a given list of numbers, numbers from the range [10; 20] are formed. The filter() function is demonstrated for a function and lambda-expression.

```# Processing of sequences in lambda expressions

# The use of lambda expressions and function filter() for a list

# 1. Declare a testable list
L = [ 8, 15, 7, 3, 11, 23, 187, -5, 20, 17 ]

# 2. Use a lambda expression to solve a problem
L2 = list(filter((lambda t: (t>=10)and(t<=20)), L))
print("L2 = ", L2) # L2 = [15, 11, 20, 17]

# 3. Use a function to solve task
def Range_10_20(t):
return (t>=10)and(t<=20)

L3 = list(filter(Range_10_20, L))
print("L3 = ", L3)```

The result of the program

```L2 = [15, 11, 20, 17]
L3 = [15, 11, 20, 17]```

### ⇑

##### 4. Processing the sequences. Using a lambda expression in the reduce() function. Example

The reduce() function allows you to get an object that is the result of some operation on a sequence (for example, the sum of the elements of a sequence). The reduce() function can be used for both functions and lambda expressions.

The general form of a function declaration is as follows

`reduce(func, sequence)`

here

• func – a function or lambda expression that specifies a formula for calculating two adjacent elements;
• sequence – processed sequence.

The reduce() function returns an object that is the result of processing.

Example. The example demonstrates using the reduce() function to calculate the sum of the elements of a sequence. To obtain a calculation formula, a lambda expression and an additional function are used.

```# Using lambda expressions to process sequences

import functools

# Using lambda expression in reduce() function

# 1. Declare a testable list
L = [ 1.88, 3, 2.4, 3.6, 4.8 ]

# 2. Calculate the sum of the elements of a list using a lambda expression
summ1 = functools.reduce((lambda a, b: a+b), L)
print("summ1 = ", summ1)

# 3. Declare a function and use it in reduce() function
return x+y

print("summ2 = ", summ2)```

The result of the program

```summ1 = 15.68
summ2 = 15.68```