Iteration helpers
Use iteration tools that express position, pairing, and ordering directly.
On this page
The concept
enumerate yields a counter and an item together; start=1 is useful for human-facing numbering. zip pairs corresponding items. With strict=False it stops at the shortest iterable. With strict=True it rejects unequal lengths, which is often safer for related records.
sorted returns a new list. A list's sort method mutates the list and returns None. reversed walks an existing order backward; it does not sort values. min, max, and sum answer different aggregate questions, and min/max need an explicit policy when data might be empty.
Worked example
names = ["Sara", "Dana", "Avan"]
scores = [90, 75, 85]
for position, name in enumerate(names, start=1):
print(position, name)
for name, score in zip(names, scores, strict=False):
print(name, score)
print(sorted(scores, reverse=True))
print(list(reversed(scores)))
print(min(scores), max(scores), sum(scores))
scores.sort()
print(scores)Trace the result
- The enumerate loop pairs display positions with names.
- The zip loop pairs each name with its corresponding score.
- Compare sorted(scores, reverse=True) with reversed(scores): only the first orders values from largest to smallest.
1 Sara
2 Dana
3 Avan
Sara 90
Dana 75
Avan 85
[90, 85, 75]
[85, 75, 90]
75 90 250
[75, 85, 90]Line-by-line walkthrough
Line 1: names = ["Sara", "Dana", "Avan"]
Evaluate ['Sara', 'Dana', 'Avan'], then bind or update names with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 2: scores = [90, 75, 85]
Evaluate [90, 75, 85], then bind or update scores with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Lines 3–4: for position, name in enumerate(names, start=1):
Obtain each item from enumerate(names, start=1) and bind it to (position, name) before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 4: print(position, name)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Lines 5–6: for name, score in zip(names, scores, strict=False):
Obtain each item from zip(names, scores, strict=False) and bind it to (name, score) before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 6: print(name, score)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 7: print(sorted(scores, reverse=True))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: print(list(reversed(scores)))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 9: print(min(scores), max(scores), sum(scores))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 10: scores.sort()
Call scores.sort to reorder the existing list. This call may have an effect even when its return value is not assigned.
Line 11: print(scores)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Plain zip can silently omit extra data. If matching lengths are part of the contract, validate lengths or choose strict=True.