Build every two-person review pair with combinations
Assigning every possible two-person review pair is an unordered-combination problem. itertools.combinations(iterable, r) produces r-length tuples in the input order, without repeated positions. With four named reviewers and r=2, the result contains six pairs: 4 choose 2. It does not emit self-pairs such as Ada-Ada, and it does not repeat the reverse of a pair such as Bo-Ada.
The example materializes the iterator because the script needs to check its count, first pair, and last pair before printing each assignment. The three assertions verify this specific fixture and ordering. The loop unpacks each tuple, formatting a clear pair label on one line. Keeping input names in a deliberate sequence also makes the displayed order predictable.
Combinations treat input positions as distinct. If the input contains the same name twice, those entries can still contribute different positional combinations, which is usually a data-quality issue for a reviewer roster. This function also does not balance workloads, exclude conflicts of interest, or schedule review sessions; it only enumerates candidate pairs. For very large rosters, the number of pairs grows quadratically, so consuming every result may be impractical even though the iterator yields results lazily. itertools.combinations() has been available since Python 2.6 and remains part of the standard library; its selection and ordering rules are in the official itertools documentation.
AI assistance disclosure: this article was drafted with AI assistance and its example was synthetically tested.
from itertools import combinations
reviewers = ["Ada", "Bo", "Cy", "Dee"]
pairs = list(combinations(reviewers, 2))
assert len(pairs) == 6
assert pairs[0] == ("Ada", "Bo")
assert pairs[-1] == ("Cy", "Dee")
for left, right in pairs:
print(f"{left}-{right}")
Ada-Bo
Ada-Cy
Ada-Dee
Bo-Cy
Bo-Dee
Cy-Dee
Comments
Post a Comment