Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Generate pair combinations from a list

Produce all unordered 2-element combinations from a list while preserving input order. Use itertools.combinations for concise code.

Python practice15 minModules & Standard LibraryIntermediateLast updated March 23, 2026

Problem statement

Given a sequence (list, tuple, or any iterable) of items, generate all unordered 2-element combinations (pairs) without repetition. Each pair should be represented as a tuple in the same order as the original elements (i < j). Return the list of pairs. If there are fewer than two items, return an empty list. The order of pairs should follow the order produced by itertools.combinations.

Task

Implement a function that returns all unique unordered pairs (as tuples) from a sequence, preserving the original order of elements.

Examples

Basic numeric list

Input

generate_pairs([1, 2, 3])

Output

[(1, 2), (1, 3), (2, 3)]

All unordered pairs from [1,2,3] in combination order.

Input format

A single call: generate_pairs(items) where items is an iterable (commonly a list).

Output format

A list of 2-tuples representing unordered pairs, e.g. [(a, b), (a, c), ...].

Constraints

Do not produce duplicate positional pairs; treat duplicate values by their positions. Use itertools.combinations or equivalent logic and preserve the order produced by combinations.

Samples

Sample 1

Input

generate_pairs(['a', 'b', 'c', 'd'])

Output

[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]

All unordered pairs from the list of characters.