Generator expressions#
Generator expressions are a high-performance, memory-efficient generalization of list comprehensions and generators.
Importing libraries and packages#
1# Mathematical operations and data manipulation
2
3# System
4from sys import getsizeof
Generator expressions#
1odd_numbers2 = [x for x in range(100000) if x % 2 != 0]
2# The bytes of memory a generator expression uses
3getsizeof(odd_numbers2)
444376
1# Surrounding the list comprehension statement with round brackets
2# instead of square ones to create a generator expression, so no
3# explicit memory has been allocated for it (lazy evaluation)
4odd_numbers = (x for x in range(100000) if x % 2 != 0)
5getsizeof(odd_numbers)
112
1# Using
2for i, number in enumerate(odd_numbers):
3 print(number)
4 if i > 10:
5 break
1
3
5
7
9
11
13
15
17
19
21
23
Single-line generator expression#
Creating a one-liner generator expression efficiently using a simple for loop
1words = [
2 "Evil\n",
3 "begins",
4 "when you begin\n",
5 "to treat people as\n",
6 "Things\n",
7]
8modified_words = (word.strip().lower() for word in words)
9list_of_words = [word for word in modified_words]
10list_of_words
['evil', 'begins', 'when you begin', 'to treat people as', 'things']
Extracting a list with single words#
1modified_words2 = (
2 w.strip().lower() for word in words for w in word.split(" ")
3)
4list_of_words2 = [word for word in modified_words2]
5list_of_words2
['evil',
'begins',
'when',
'you',
'begin',
'to',
'treat',
'people',
'as',
'things']
1# Equivalent code using a nested for loop
2modified_words3 = []
3for word in words:
4 for w in word.split(" "):
5 modified_words3.append(w.strip().lower())
6modified_words3
['evil',
'begins',
'when',
'you',
'begin',
'to',
'treat',
'people',
'as',
'things']
Independent for loops in a generator expression#
1marbles = ["RED", "BLUE", "GREEN"]
2counts = [1, 5, 13]
1# Generating all possible combinations of the values in the marbles
2# array and counts array
3marble_with_count = ((m, c) for m in marbles for c in counts)
4
5# This generator expression creates a tuple in each iteration of the for loops.
6marble_with_count_as_list_2 = []
7for m in marbles:
8 for c in counts:
9 marble_with_count_as_list_2.append((m, c))
10marble_with_count_as_list_2
[('RED', 1),
('RED', 5),
('RED', 13),
('BLUE', 1),
('BLUE', 5),
('BLUE', 13),
('GREEN', 1),
('GREEN', 5),
('GREEN', 13)]