Python Tip of the Day: Save Memory with Generators

Save Memory with Generators

Author: Jeremy Morgan
Published: November 8, 2024


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.


Generators yield items one at a time, which is great for large datasets.

# Generator function
def count_up_to(max):
    count = 1
    while count <= max:
        yield count
        count += 1

for number in count_up_to(5):
    print(number)
# Output:
# 1
# 2
# 3
# 4
# 5

“Python Tip of the Day: Save Memory with Generators”


The Python Tip of the Day is a daily series published in the month of November. The tips are designed to help you become a better Python programmer. I post tips like this and more every single day on X. Let’s connect!


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.