A generator function is a Pythonic way to create an iterable without explicitly storing it in memory. This reduces memory usage of your code without incurring any additional costs.
import random # NOT A GENERATOR! # Create and return a list of numbers def get_numbers(n): numbers =  for i in range(n): numbers.append(random.random()) # List of n elements exists in memory return numbers # Sum up 1000 random numbers s = 0 for x in get_numbers(1000): s += x print(s)
However, this is not very efficient code because you create a list in advance without need. What if you had 1,000,000,000 numbers? Your memory would quickly fill up!
A better way is to use a generator function with the
yield keyword that creates the random numbers dynamically as they are iterated over:
import random # GENERATOR # Generate numbers one by one def generate_numbers(n): for i in range(n): yield random.random() # Sum up 1000 random numbers s = 0 for x in generate_numbers(1000): s += x print(s)
There are two big advantages to using a generator:
- (1) You don’t have to create a huge list first and store it in memory but generate the next element as you iterate over it.
- (2) It’s shorter and more concise.
However, it may not be concise enough for you! 😉 So, here’s the problem addressed in this article:
Problem: Can we write a one-line generator?
Let’s dive into different methods to accomplish this!
Method 1: One-Liner Generator Function
print(sum(random.random() for i in range(1000)))
The code consists of the following parts:
print()function prints the result of the expression to the shell.
sum()function sums over all values in the following iterable.
- The generator expression
random.random() for i in range(1000)generates 1000 random numbers and feeds them into the outer
sum()function without creating all of them at once.
This way, we still don’t store the whole list of 1000 numbers in memory but create them dynamically.
Method 2: Generator Expression in Parentheses
By passing a statement of the form
(expression context) into the parentheses
(), such as
(x**2 for x in range(10)), you create a generator object that modifies each element of an iterator specified in the context. In our example, the generator would create the first 10 square numbers.
But it doesn’t actually instantiate these numbers right away to remain memory-efficient as you can see here:
numbers = (x**2 for x in range(10)) print(numbers) # <generator object <genexpr> at 0x0000021FB07F3EB0>
The numbers are not yet created, only the “blueprint” of how the numbers will be created if you force Python to do that.
Here, you iterate over the generator and print all ten numbers:
for i in numbers: print(i)
0 1 4 9 16 25 36 49 64 81
👉 Recommended Tutorial: Generator Expressions in Python
Method 3: exec()
The following method is not pretty—but it solves the problem of creating a generator in a single line of code.
exec('def g(n):\n for i in range(n):\n yield random.random()')
💡 Just pass the code you want to run as a string and replace all newlines with the newline character
'\n'. This way, you can create a generator function
g(n) that dynamically creates
n random numbers.
You can now iterate them using the standard code snippet:
s = 0 for x in g(1000): s += x print(s) # 488.318368852096
Because the numbers are random, the output will be different for you. You can try it yourself in our interactive shell:
Exercise: What’s the output for you? Why is it different than ours?
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.
Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.
You’ll also learn how to:
- Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
- Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
- Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
- Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
- Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.