Skip to content

Iteration

Repeating a computational step, often over data items. Understand Python loops, iterable versus iterator, and why consuming a market-data iterator twice can lose a pass.

Iteration is the repeated execution of a computational step, often with a different data item or an updated state on each pass. A portfolio valuation iterates through positions; a numerical solver iterates through estimates. The practical question is what advances between passes, and what makes the repetition stop.
In Python, two common forms are for, which requests successive items from an iterable, and while, which repeats while a condition remains true. Neither spelling guarantees that a calculation finishes: a source can be unbounded, and a condition can remain true indefinitely. Python's language reference specifies these control-flow rules.

A pass through two positions

Suppose two fictional positions have market values of 200 USD and 150 USD at the same valuation time. This loop accumulates their total:

position_values = [200, 150] # fictional USD values at one valuation time total = 0 for value in position_values: total += value print(total) # 350
The list supplies 200, then 150. The body runs once for each supplied value, leaving a total of 350 USD. An empty list would give zero body executions and leave total at its initial value of zero. Iteration describes the process of visiting the items; it does not require storing a new collection of results.

The iterable is not the cursor

An iterable is an object from which Python can obtain an iterator. An iterator tracks progress through successive items: next(cursor) requests the next item, and exhaustion is signalled by StopIteration. A for loop handles that exhaustion normally. These are the documented iterator rules.

The distinction matters when two calculations receive the same input object:

position_values = [200, 150] cursor = iter(position_values) print(sum(cursor)) # 350 print(list(cursor)) # [] print(list(position_values)) # [200, 150]
sum has consumed the cursor. The empty result on the next line means there are no items left in that iterator; it does not mean the portfolio had no positions. The source list is unchanged. A fresh loop over that list can start again, whereas calling iter(cursor) returns the same cursor and does not rewind it. Every iterator is iterable, so “iterable” alone does not promise repeatable passes. See Python's iterable and iterator definitions.
Fictional position values 200 and 150 USD remain in the source list. sum(cursor) returns 350 USD and exhausts the cursor; list(cursor) then returns an empty list, while list(position_values) returns [200, 150].
Fictional position values 200 and 150 USD remain in the source list. sum(cursor) returns 350 USD and exhausts the cursor; list(cursor) then returns an empty list, while list(position_values) returns [200, 150].
Original fictional example at one valuation time; amounts are USD. The same exhausted cursor cannot restart, but a new pass over the retained list can read both values again.
A generator is an iterator too. A generator function uses yield to suspend and resume computation as items are requested; it need not compute every result in advance. Once that generator is exhausted, another pass needs a new generator or retained output. Python's generator tutorial explains this suspended state.

Before iterating through market events

In stream processing, a diagnostic that drains a one-pass event iterator can leave a later calculation with nothing to process. Combine the needed statistics in one pass, retain an appropriately bounded dataset, or use a source with an explicit replay facility. Turning an unbounded stream into a list is not a workable retention policy.

Also check the source's ordering contract. A Python list is traversed in its sequence order; iteration in general does not promise chronological market events. An event-driven backtester must define how timestamps and event ordering are handled. A loop cannot repair an ambiguous feed merely by visiting every item.

For hands-on loop syntax, Python Syntax and Control Flow applies a position rule to several signals. The full lesson is free with a signed-in account.

Sources

Examples checked with Python 3.14.4.

Last updated
  • python
  • iteration
  • loops
  • iterators
  • stream processing
← Back to the glossaryBack to top ↑