If a Python generator produces values once and then appears empty, it has probably been exhausted. A generator object is a one-pass iterator, not a reusable collection: to iterate again, create a new generator and, when necessary, a new underlying data source—or save finite results in a list.
Why a generator is empty on the second pass
A generator function and a generator object are different things. A function containing yield creates a generator object when called; it does not immediately run through the function and return all its values. Each call to next(), or each step of a loop, resumes that object until it yields a value. When the function returns or reaches its end, the iterator signals that it is finished with StopIteration. That is the normal end-of-iteration signal, not an error by itself. See the Python language reference on yield expressions and the built-in exception documentation.
def numbers():
yield 1
yield 2
g = numbers()
print(list(g)) # [1, 2]
print(list(g)) # [] — g has already been exhausted
list(g) consumes the values from g. So does a for loop, sum(), or another operation that reads the iterator. After the first consumer reaches the end, the same generator object has no built-in rewind operation. Calling iter(g) does not recreate its execution state; it returns the iterator itself. The iterator behavior is described in the Python built-in functions documentation.
How to iterate again
Call the generator function again
If the generator’s inputs can be recreated, call its function again for a fresh generator object:
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
def numbers():
yield 1
yield 2
first_pass = list(numbers())
second_pass = list(numbers())
Each call to numbers() creates a separate iterator and runs the generator body as that iterator is consumed.
Store finite results when reuse is needed
If you need to revisit the same finite results and they fit comfortably in memory, materialize them once:
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
items = list(make_items())
for item in items:
process(item)
for item in items:
compare(item)
The list is reusable; the generator used to create it is still consumed. Avoid this approach when the results are too large to hold comfortably in memory or are unbounded.
Recreate the underlying source too
A new outer generator is not enough if it wraps an already-consumed iterator. For example, if a generator reads from a file, cursor, or other one-shot source, a second pass needs a new source as well as a new wrapper. Reopen the file or rerun the source-producing operation when that is appropriate. If the source cannot be reproduced cheaply or has side effects, consider changing the algorithm to perform both operations in one pass or using a source-specific way to query it again.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Choose a reuse pattern that fits the source
| Pattern | Use it when | Trade-off |
|---|---|---|
| Create a new generator | The inputs and source can be recreated cheaply and repeatably. | Recomputes the results on each pass. |
Materialize with list() |
The results are finite, needed more than once, and fit comfortably in memory. | Stores the full result in memory. |
| Reopen or recreate the source | The generator reads from a one-shot source such as a file or cursor. | Requires a fresh source; wrapping the consumed iterator again does not restore its data. |
| Change the algorithm | The source is large, unbounded, costly to reproduce, or has side effects. | May require combining operations into one pass or using a source-specific repeat query. |
The choice depends on whether the source can be reproduced, whether all results fit in memory, the cost of recomputing them, and any side effects or external state involved.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What StopIteration and RuntimeError mean
StopIteration tells Python’s iterator protocol that there is no next value. A for loop handles it internally and ends the loop. If you call next(g) directly after the iterator is exhausted, the exception reaches your code. Use next(g, default) when you want a default instead:
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
value = next(g, None)
Choose a unique sentinel instead of None if None could itself be a valid value.
Use return to finish a generator normally
Do not use raise StopIteration as a generator’s normal way to finish. Let the function reach its end or use return. Under PEP 479, an unhandled StopIteration escaping from a generator body is converted to RuntimeError; Python enabled this behavior for all code in version 3.7. If a direct next() inside the generator is expected to run out, catch the exception at that call site:
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
def take_two(iterator):
for _ in range(2):
try:
value = next(iterator)
except StopIteration:
return
yield value
This example ends the generator normally if its input has fewer than two remaining values.
Quick Recap
Debug a generator that unexpectedly has no values
- Check whether the variable has already been passed to
list(),sum(), a loop, or another consumer. - Find the first place it was advanced. A diagnostic
next(g)consumes a value; it is not a peek. - Check whether the generator wraps an iterator that was itself already consumed.
- For another pass, recreate the original source and generator, or deliberately store finite results if they fit in memory.
- If the traceback says
RuntimeError: generator raised StopIteration, inspect the generator for an uncaughtnext()or explicitraise StopIteration. Catch expected exhaustion or usereturn.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

