# Python iterator & mutable default explorer

> Trace two classic Python state bugs side by side: one-shot generator exhaustion across two passes and shared mutable default list arguments across function calls.

- Canonical HTML: [https://unrust.dev/tools/python-iterator-default-explorer](https://unrust.dev/tools/python-iterator-default-explorer)
- Markdown representation: [https://unrust.dev/tools/python-iterator-default-explorer/index.md](https://unrust.dev/tools/python-iterator-default-explorer/index.md)

## How to use the tool

Switch between the two-pass iterator trace and the mutable default argument trace. Change the container or function signature and inspect what state survives into the next operation.

## Worked example

In Python, any(n >= 18 for n in records) consumes 12 and stops when 18 matches. Because records is a generator iterator, sum(records) resumes from the cursor and sums only the remaining 25. Converting the input to a sequence or materializing a list preserves all three items for the second pass.

```text
records = (n for n in [12, 18, 25])
has_adult = any(n >= 18 for n in records)
total = sum(records)  # 25, not 55
```

Inputs stay in your browser. These are open learning examples, not scored diagnostic drills.

[Trace exhausted Python iterators and shared mutable defaults](https://unrust.dev/blog/python-exhausted-iterators-and-mutable-defaults/index.md)

[Python docs: default argument values and iterators](https://docs.python.org/3/tutorial/controlflow.html#default-argument-values)

## Practice spotting stateful inputs in Python

A helper accepts an iterable of numbers, checks if any value is negative with any(x < 0 for x in items), and then averages items. Which input type breaks the average?

Passing a generator or file iterator breaks the second pass because any() advances the iterator through every non-negative item until it stops. If all items are non-negative, the iterator is completely exhausted before the average calculation begins.

Original open example, not a scored drill. Close tools before an unassisted session.

[Try the Python iterator consumption challenge](https://unrust.dev/guides/python-iterator-consumption-challenge/index.md)

[Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
