Python. List generators

List generators. Basic concepts


Contents


Search other resources:

1. List generators. Concepts. General form

List generators are a special way of implementing a for loop that generates a list based on data and actions performed on that data. List generators are a mechanism (method) for obtaining a list based on some rule (set of rules). List generators can be combined with functional programming elements such as map() and filter() functions.

The most general form of a list generator with one counter variable is as follows

[ expression for variable in sequence [if cond] ]

here

  • expression is an expression that evaluates over each element of the sequence. The result is a new sequence in the form of a list;
  • variable – a counter variable that in turn takes values from the sequence;
  • sequence – a sequence of elements. It can be a list, a tuple, a set;
  • cond is a conditional expression in the if statement that can be used to form a new list according to the condition.

The general form of any list generator for an arbitrary number of nested loops is

[ expression for variable1 in sequence1 [if cond1]
             for variable2 in sequence2 [if cond2]
             ...
             for variableN in sequenceN [if condN] ]

here

  • expression – some expression calculated over each element of the sequence. If the expression contains a counter variable variable1, variable2, variableN, then each time the value of this variable is calculated in accordance with the sequence sequence1, sequence2, sequenceN;
  • variable1, variable2, variableN – counter variables that alternately take values in accordance with the sequences sequence1, sequence2, sequenceN;
  • sequence1, sequence2, sequenceN – some sequences (sets) of data. These sequences can be lists, tuples, sets;
  • cond1, cond2, condN – conditional expressions according to Python syntax, which are used in the if statement.

 

2. An example of a basic list generator that processes a list, tuple, and set

The example demonstrates the use of a list generator for objects that are lists, tuples and sets.

# List generators

# Task.
# For a given set of integers, create a list
# in which all elements are squared

# 1. Solution for a list of numbers.
# 1.1. Specified list of numbers
L = [ 34, 76, 12, 35 ]

# 1.2. Write the generator, get the result
res_L = [ x*x for x in L ]

# 1.3. Display the result
print("res_L = ", res_L) # res_L = [1156, 5776, 144, 1225]

# 2. The solution for a tuple
# 2.1. The specified tuple
T = ( 11, 77, 23, 14, 20)

# 2.2. Write the generator, get the result
res_T = [ x**2 for x in T ]

# 2.3. Display the result
print("res_T = ", res_T) # res_T = [121, 5929, 529, 196, 400]

# 3. Solution for the set
# 3.1. The specified set
S = { 2, 2, 8, 3, 3, 4, 5}

# 3.2. Form a generator that will multiply the elements of the set by 5
res_S = [ 5*t for t in S ]

# 3.3. Display the result, taking into account
#      that the elements in the set are not repeated
print("res_S = ", res_S) # res_S = [40, 10, 15, 20, 25]

The result of the program

res_L = [1156, 5776, 144, 1225]
res_T = [121, 5929, 529, 196, 400]

 

3. Replacing the list generator with other Python tools. Examples

The list generator can be replaced by other means of the Python language, namely:

  • using basic language operators like for, while, if and the like;
  • by the map() function, if it is required to process the elements of the set without additional conditions. As you know, the map() function reflects some implemented function onto a sequence;
  • by the filter() function, if it is required to process the elements of the set according to some condition if. In this case, when forming a new list from the original sequence, the elements whose values in the if statement return False, are skipped.

However, in this case, the advantage of the list generator is simpler program code that is easier to understand.

 

3.1. Replacing the list generator with a combination of basic for, while statements. Example

Any algorithm in Python can be implemented using the standard language operators for, while, if, and the like.

Task. A list of words containing the ‘_’ (underscore) character is given. Create a new list, in which the underscore character in the words ‘_’ is replaced by the character ‘ ‘ (space).

Solution. The solution using a list generator

# Solution with a list generator

# The initial set of words
L = [ "ab_cd_e", "abc", "a_b_c", "a__bc_d_", "__" ]

# Construct a list generator
resL = [ x.replace('_', ' ') for x in L ]

# Display a new list
print("resL = ", resL) # resL = ['ab cd e', 'abc', 'a b c', 'a bc d ', '   ']

In the above snippet, the list generator uses the replace() function to replace a character.

Solution using a for loop and an if statement

# The initial set of words
L = [ "ab_cd_e", "abc", "a_b_c", "a__bc_d_", "__" ]

resL = []
for x in L:
    s = ""
    for c in x:
        if (c == '_'):
            s = s + ' '
        else:
            s = s + c
    resL = resL + [s]

# Display the result
print("resL = ", resL)

Another solution is possible using replace() function

# The initial set of words
L = [ "ab_cd_e", "abc", "a_b_c", "a__bc_d_", "__" ]

# Form a new list using the replace() function
resL = []
for x in L:
    resL = resL + [x.replace('_', ' ')]

# Display the result
print("resL = ", resL)

Solution using a while loop, if statement, and replace() function

# The initial set of words
L = [ "ab_cd_e", "abc", "a_b_c", "a__bc_d_", "__" ]

# Form a new list using the while, if statements
# and the replace() function
resL = L
i = 0
while i < len(L):
    resL[i] = resL[i].replace('_', ' ')
    i = i+1

# Display the result
print("resL = ", resL)

 

3.2. Replacing the list generator with the map() function. Example

If you need to impose some function on the elements of the list, then you can use the map() function for this.

Task. A list of words containing the ‘_’ (underscore) character is given. Create a new list, in which the underscore character in the words ‘_’ is replaced by the character ‘ ‘ (space).

 Solution. To solve the problem, the map() function is used, which imposes the replace() function of replacing the symbol on each element of the list.

# The specified set of words
L = [ "ab_cd_e", "abc", "a_b_c", "a__bc_d_", "__" ]

# Form a new list using the capabilities of the map() function
resL = list(map((lambda x: x.replace('_', ' ')), L))

# Display the result
print("resL = ", resL)

The solution to this problem using the list generator is described in section 4.1.

 

3.3. Replacing the list generator with the filter() function. Example

The filter() function is useful when you need to get a new list that is formed using some condition.

Task. A set of words is given. Generate a new list containing words longer than 4 characters.

Solution. Solving a problem using a list generator

# Comparing list generators with the filter function

# A set of words (strings) is given.
# Select words with more than 4 characters.

# Original word set
L = [ "abcde", "123", "12345", "qwerty", "XXL" ]

# Generate a new list using the list generator
resL = [ x for x in L if len(x)>4 ] # resL = ['abcde', '12345', 'qwerty']

# Display the reult
print("resL = ", resL)

Solving the problem using the filter() function

# Original word set
L = [ "abcde", "123", "12345", "qwerty", "XXL" ]

# Form a new list using the filter() function
resL = list(filter((lambda x: len(x)>4), L))

# Display the result
print("resL = ", resL)

 

4. List generator in which if checks are added. Examples
4.1. Generate a list with elements that are multiples of 5

Task. Using the list generator, for a given list of numbers, create a new list in which each element is a multiple of 5.

Solution. The text of the program in Python is as follows.

 

# List generators

# Task.
# For the initial set of integers, create a list
# in which all elements of the set are multiples of 5.

# 1. Original list of numbers
L = [ 34, 76, 12, 35, 20, 18, 11, 55, 5 ]

# 2. Generate a list generator, get the result
res = [ item for item in L if item % 5 == 0 ]

# 3. Display the result
print("res = ", res) # res = [35, 20, 55, 5]

As you can see from the above code, to determine if a list item is a multiple of 5, the condition check is used

if item % 5 == 0

This condition allows you to filter out numbers that are not multiples of 5. As a result, the program will produce the following result

res = [35, 20, 55, 5]

 

4.2. Form a list with elements. Solution using list generator, for loop and filter() function

 Task. For a given list of strings, generate a new list in which each string has two letters “z”. To solve the problem, use the following methods:

  • using the list generator;
  • using the standard for loop;
  • using the filter() function.

Solution. To calculate the number of occurrences of a character (substring) in a string, use the standard count() function.

The text of the program that solves this problem is as follows

# List generators

# Task.
# For a given set of strings, create a new list
# in which all elements have two letters 'z'

# 1. Original string list
L = [ 'abcz', 'zzz', 'zz', 'azaz', 'abz', 'zabcz', 'azaazab' ]

# 2. The solution using a list generator.
res1 = [ s for s in L if s.count('z')==2 ] # use the count() function

# 3. Solution with a standard for loop
res2 = []
for s in L:
    if s.count('z') == 2:
        res2.append(s)

# 4. Solution with filter() function
res3 = list(filter((lambda s: s.count('z') == 2), L))

# 5. Display the result
print("res1 = ", res1) # res1 = ['zz', 'azaz', 'zabcz', 'azaazab']
print("res2 = ", res2) # res2 = ['zz', 'azaz', 'zabcz', 'azaazab']
print("res3 = ", res3) # res3 = ['zz', 'azaz', 'zabcz', 'azaazab']

Program result

res1 = ['zz', 'azaz', 'zabcz', 'azaazab']
res2 = ['zz', 'azaz', 'zabcz', 'azaazab']
res3 = ['zz', 'azaz', 'zabcz', 'azaazab']

 


Related topics