# Using standard functions in combination with generator expressions. Functions sum(), sorted(), map(), all(), any(), min(), max(), filter(), zip(), enumerate()

Before exploring this topic, it is recommended that you familiarize yourself with the following topic:

### Contents

Search other resources:

##### 1. Function sum(). Calculating the sum of the elements of a sequence

The sum() function calculates the sum of the elements in a sequence. If a generator expression is used in the body of the sum() function, then the general form of such a combination is as follows

```IterObj = ( expression )
result = sum(IterObj)```

here

• expression – expression generator;
• IterObj – an iteration object obtained using a generator expression;
• result – resulting sum.

When calling the sum() function, it is allowed to directly place a generator expression within parentheses. In this case, the parentheses in which the declared generator expression can be omitted.

Example. Demonstrates the calculation of the sum using the sum() function for various generator expressions.

```# Generator expressions and the sum() function

# Calculating the sum of list items
L = [ 1, 3, 5, 8, 2, 4 ]
res = sum((t for t in L))
print("res = ", res)

# Calculating the sum 2 + 4 + 6 + ... + 100
sum_2_100 = sum(i for i in range(2, 101, 2)) # parentheses () can be omitted
print("2 + 4 + 6 + ... + 100 = ", sum_2_100)

# Calculating the sum 0.1 + 0.3 + 0.5 + ... + 2.0
IterObj = (i*0.1 for i in range(1, 21, 2)) # get an iterator object
sum_01_20 = sum(IterObj)
print("0.1 + 0.2 + ... + 2.0 = ", sum_01_20)```

Program result

```res = 23
2 + 4 + 6 + ... + 100 = 2550
0.1 + 0.2 + ... + 2.0 = 10.000000000000002```

### ⇑

##### 2. Function sorted(). Sorting items in a sequence

The sorted() function sorts a set of values. A sorted set of values is returned using a list. Using the sorted() function with generator expressions has the following common uses

```IterObj = ( expression )
resIterable1 = sorted(IterObj) # sorting in ascending order
resIterable1 = sorted(IterObj, reverse = True) # descending sort```

here

• expression – expression generator;
• IterObj – an iterator object that supports the iteration protocol. This object is constructed from expression;
• resIterable1, resIterable2 – lists that are the results of the sorted() function.

Example.

The example demonstrates the use of the sorted() function to sort a list and tuple using an expression generators. Pay attention to the following: if a generator expression is directly passed to a function, then it must be surrounded by parentheses (the case with a tuple).

```# Expression generators and function sorted()

# 1. Sorting the list items
# 1.1. A list
L = [ 1, 3, 5, 8, 2, 4 ]

# 1.2. Create an iterator object using a generator expression
IterObj = ( t for t in L )

# 1.3. Sort items in descending order (reverse = True),
# get the resulting list
sortedL = sorted(IterObj, reverse = True)
print("L = ", L)
print("sorted(L) = ", sortedL)

# 2. Sorting elements of a tuple
# 2.1. The specified tuple
T = ( 1.4, 3.4, -2.8, 9.1, 0.7 )

# 2.2. Sort the elements of a tuple,
# set directly sorting in the list in ascending order
sortedT = sorted(( t for t in T ), reverse = False)

# 2.3. Display the result
print("T = ", T)
print("sotred(T) = ", sortedT)```

Program result

```L = [1, 3, 5, 8, 2, 4]
sorted(L) = [8, 5, 4, 3, 2, 1]
T = (1.4, 3.4, -2.8, 9.1, 0.7)
sotred(T) = [-2.8, 0.7, 1.4, 3.4, 9.1]```

### ⇑

##### 3. Function all(). Determining if all elements of a sequence satisfy a given condition

The all() function returns True if all elements of the iterated object are true values, that is, satisfy the specified condition. In combination with a generator expression, the general form of using the function is as follows

```IterObj = ( expression )
result = all(IterObj)```

here

• expression – expression generator;
• IterObj – an iterator object that is the result of a generator expression;
• result – boolean value. If all elements of a sequence (list, tuple, set, etc.) satisfy a certain condition, then result = True. Otherwise, result = False.

Example.

```# Generator expressions and the all() function

# 1. Using with lists
# 1. Specified list
L = [ 11, 3, 5, 10, 12, 4 ]

# 1.2. Determine if all numbers in the list are greater than 2
res = all( item > 2 for item in L )
print("L = ", L)
print("L(>2) = ", res)

# 1.3. Determine if all numbers in the list are paired
res2 = all ( item%2 == 0 for item in L)
print("L(%2==0) = ", res2)

# 2. Using with tuples
# 2.1. Specified tuple
T = ( 'abcd', 'abcd', 'abd', 'ade' )

# 2.1. Determine if all lines of a tuple begin with the letter 'a'
res3 = all ( s == 'a' for s in T )
print("res3 = ", res3)```

### ⇑

##### 4. Function any(). Determining whether at least one element of the sequence satisfies a given condition

The any function returns True if one of the elements in the set satisfies the specified condition. The general form of using a function in conjunction with a generator expression is as follows:

```IterObj = ( expression )
result = any(IterObj)```

here

• expression – generator expression;
• IterObj – an iterator object that supplies a value to a function;
• result – a result that can be one of two values True or False. If one element from the set satisfies the given condition, then result = True.
```# Function any() - determining whether at least one element satisfies a given condition

# 1. Determine if the given list contains zero elements
L = [ 2, 3, 8, 1, 3, 4, 2, 7 ]
res = any( item == 0 for item in L )
print(res)

# 2. Determine if the given list contains elements '+'
LS = [ 'abc', 'cd+e', 'ab', 'a-b', 'fgh' ]
res = any( ('+' in s) for s in LS )
print(res)```

Program result

```False
True```

### ⇑

##### 5. Functions min(), max(). Determining the maximum and minimum values of a sequence

The min() and max() functions are used to determine the maximum and minimum values of a sequence. In combination with generator expressions, the general form of function declarations is.

```IterObjMin = ( exprMin )
resMin = min(IterObj)

IterObjMax = ( exprMax )
resMax = max(IterObj)```

here

• exprMin, exprMax – generator expressions;
• IterObjMin, IterObjMax – iterator objects that supply a value for the min() and max() functions, respectively;
• resMin, resMax – respectively the minimum and maximum value in the sequence.

Example.

```# Function min() - determine the minimum value in a sequence
L1 = [ 12.88, 3.8, 4.22, 11.6, 3.99 ]
resMin = min( item for item in L1 )
print("L1 = ", L1)
print("min(L1) = ", resMin)

# Function max() - determine the maximum value of a sequence
L2 = [ 5.2, 3.8, 4.1, 6.2, 4.4 ]
resMax = max( item for item in L2)
print("L2 = ", L2)
print("max(L2) = ", resMax)```

Program result

```L1 = [12.88, 3.8, 4.22, 11.6, 3.99]
min(L1) = 3.8
L2 = [5.2, 3.8, 4.1, 6.2, 4.4]
max(L2) = 6.2```

### ⇑

##### 6. Function map(). Executing a function on each element of the sequence

The map() function implements the execution of some function for each element of the sequence. Sequence elements can be obtained using a generator expression. In this case, the general form of using the map() function is

`map( FuncName, ( expression ))`

here

• expression – expression generator;
• FuncName – the name of the function to execute on each element supplied by the generator expression.

Example 1.

The example, for a given list of strings, reverses each string by combining the map() function and a generator expression.

```# Function map() - execution of some function on each element of the sequence

# Task. Given a list of strings.
# Reverse each line in the list.

# 1. Declaring your own function that performs string reversal
def Reverse(s):
s_reverse = ''
for c in s:
s_reverse = c + s_reverse
return s_reverse

# 2. Specified list
LS = [ 'abcd', 'def', 'ghi', 'jkl' ]

# 3. Calling the map function, getting the resulting list
# A generator expression is used here
LS2 = list(map(Reverse, ( s for s in LS )))

# 4. Display the result
print(LS)
print(LS2)```

Program result

```LS = ['abcd', 'def', 'ghi', 'jkl']
Reverse(LS) = ['dcba', 'fed', 'ihg', 'lkj']```

Example 2.

Task. Given an initial list of integers L1. Form a new list L2, in which each element is equal to the sum of the digits of the corresponding element of the list L1. For the solution, use a combination of the map() function and a generator expression.

Solution.

```# Function map() - execution of some function on each element of the sequence

# Task. Create a new list in which each element is equal
# to the sum of the digits of the original list

# 1. Declaring your own function that returns the sum of the digits of number,
# for example: 23489 => 26
def SumDigits(number):
summ = 0
i = 0
t = number
while t>0:
summ += int(t%10)
t //= 10
return summ

# 2. A specified list of positive integers
L1 = [ 1389, 230, 77, 43409 ]

# 3. Calling the function map, getting the resulting list
L2 = list(map(SumDigits, ( item for item in L1 )))

# 4. Display the result
print("L1 = ", L1)
print("L2 = ", L2)```

Program result

```L1 = [1389, 230, 77, 43409]
L2 = [21, 5, 14, 20]```

### ⇑

##### 7. Function filter(). Based on the given sequence, get an iterated object according to some criterion

The filter() function returns an iterated object that is a sequence of elements that is formed based on a specified condition. The general form of using filter() with a generator expression is as follows:

`IterObj = filter( FuncName, ( expression ) )`

here

• expression is a generator expression that returns an iterated object;
• FuncName – a function to be applied to each element of the sequence generated by the generator expression.

Example. The example shows how to use the filter() function and a generator expression to get a list of items that have a value greater than 5.

```# The filter() function - returns an iterator for a sequence of elements
# that satisfy a given condition.

# 1. A function that determines whether the number num is greater than 5
def GreateThan5(num):
return num>5

# 2. Specified list
L = [ 2, 15, 8, 1, 4 ]

# 3. Generate an iterated object based on the GreateThan() function
IterObj = filter( GreateThan5, ( item for item in L ) )

# 4. Create new list based on IterObj object
L2 = list(IterObj)
print(L2)```

Program result

`[15, 8]`

### ⇑

##### 8. Function zip(). Combining elements of multiple sequences in a tuple

The zip() function is used to combine the elements of multiple sequences in a tuple. In the most general case, using the zip() function might be something like this

```IterObj1 = ( expression1 )
IterObj2 = ( expression2 )
...
IterObjN = ( expressionN )

ResObj = zip( IterObj1, IterObj2, ..., IterObjN )```

here

• expression1, expression2, expresssionN – generator expressions that form iterated objects;
• IterObj1, IterObj2, IterObjN – iterated objects that supply the value;
• ResObj – the resulting iterable object. This object contains a sequence of tuples, each of which is a union of elements from the iterated objects IterObj1, IterObj2, IterObjN.

The result obtained by the zip() function can be converted to a list, set, or tuple. For example, to get the result as a tuple, you need to call the following code

`ResTuple = tuple(ResObj)`

Example.

The example uses the zip() function and a generator expression to form a ResList that combines three lists L1, L2, L3.

```# Function zip() - concatenation of elements of different sequences in a tuple

# 1. Three lists are given
L1 = [ 'ab', 'cd', 'ef', 'gh' ]
L2 = [ 2.88, 3.55, 4.11 ]
L3 = [ 10, 20, 30, 40, 50, 60 ]

# 2. Get iterated objects using generator expressions
IterObj1 = ( item for item in L1 )
IterObj2 = ( item for item in L2 )
IterObj3 = ( item for item in L3 )

# 3. Call zip() function on iterated objects,
# get the resulting object
Res = zip(IterObj1, IterObj2, IterObj3)

# 4. Generate a list of results
ResList = list(Res)

# 5. Display the result
print("L1 = ", L1)
print("L2 = ", L2)
print("L3 = ", L3)
print("IterObj1 = ", IterObj1)
print("IterObj2 = ", IterObj2)
print("IterObj3 = ", IterObj3)
print("zip(...) = ", ResList)```

Program result

```L1 = ['ab', 'cd', 'ef', 'gh']
L2 = [2.88, 3.55, 4.11]
L3 = [10, 20, 30, 40, 50, 60]
IterObj1 = <generator object <genexpr> at 0x034D14F0>
IterObj2 = <generator object <genexpr> at 0x034D1630>
IterObj3 = <generator object <genexpr> at 0x034D19B0>
zip(...) = [('ab', 2.88, 10), ('cd', 3.55, 20), ('ef', 4.11, 30)]```

### ⇑

##### 9. Function enumerate(). Numerate the items in the sequence

The enumerate() function loops through all the elements in a sequence and returns a set of tuples. Each tuple contains a pair consisting of the ordinal number of an element in the sequence and the value of that element in the sequence.
In the most general case, using the enumerate() function with a generator expression is

`IterObj = enumerate( expression )`

here

• expression – expression generator;
• IterObj – iterable object.

After obtaining an iterated object that will supply the value, you can form the result in the form of some sequence (list, tuple, set). If, for example, you need to form a set of ResSet, then the following code will be called

`ResSet = set(IterObj)`

Example. The example builds a list of tuples. Each tuple is a pair that defines the sequence number and value of the tuple.

```# Function enumerate() - splits a sequence into tuples.

# 1. Specified list
L = [ 'ab', 'cd', 'ef', 'gh' ]

# 2. Get an iterated object, call the enumerate() function
#    combined with a generator expression
IterObj = enumerate( item for item in L1 )

# 3. Get a list of tuples
ResList = list(IterObj)

# 4. Display the result
print("L = ", L)
print("IterObj = ", IterObj)
print("enumerate(L) = ", ResList)```

Program result

```L = ['ab', 'cd', 'ef', 'gh']
IterObj = <enumerate object at 0x03851558>
enumerate(L) = [(0, 'ab'), (1, 'cd'), (2, 'ef'), (3, 'gh')]```