MyInternships.in

Python Functions

Python Generators

A generator is a special kind of function that produces values one at a time, on demand, instead of building and storing an entire list in memory at once — making it far more memory efficient for large sequences.


The yield Keyword

A generator function looks like a normal function but uses yield instead of return. Each time yield runs, the function pauses, hands back a value, and remembers exactly where it left off, ready to continue from that point the next time a value is requested.

A simple generator function
Python
def count_up_to(n):
    i = 1
    while i <= n:
        yield i
        i += 1

for number in count_up_to(5):
    print(number)
# 1 2 3 4 5, one at a time

Lazy Evaluation

Generators are "lazy" — they compute the next value only when it is actually asked for, instead of computing every value up front. Calling a generator function does not run its body at all; it just creates a generator object.

Nothing runs until you ask
Python
gen = count_up_to(3)
print(gen)          # <generator object ...>

print(next(gen))    # 1
print(next(gen))    # 2
print(next(gen))    # 3
# next(gen) again -> raises StopIteration

Generator Expressions

A generator expression looks just like a list comprehension but uses parentheses () instead of square brackets []. It builds a generator instead of a full list, which is a quick way to get laziness without writing a whole function.

Generator expression
Python
squares_list = [x * x for x in range(1000000)]   # builds full list in memory
squares_gen = (x * x for x in range(1000000))    # builds nothing yet

print(next(squares_gen))   # 0
print(next(squares_gen))   # 1

Memory Efficiency vs Lists

A list stores every single element in memory at once. A generator stores only its current position and the logic to produce the next value, so it uses a tiny, constant amount of memory no matter how large the sequence is — ideal for reading huge files or streams of data.

AspectListGenerator
Memory usageStores all items at onceStores one item at a time
Speed to createSlower for huge sequencesInstant — nothing computed yet
Re-usable?Yes, can loop many timesNo, exhausted after one pass
Indexing (data[2])SupportedNot supported
💡

Use a generator when you only need to loop through data once, especially for very large datasets — use a list when you need to access items by index or loop over the same data multiple times.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships