tuples
subtuples
Python programming
data structures
algorithms

subtuples for a tuple

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

When people ask for subtuples, they usually mean one of two things: contiguous slices taken from a tuple, or smaller tuples formed by choosing elements from the original tuple. The distinction matters because the implementation and output size are different, and Python gives you good tools for both.

Contiguous Subtuples With Slicing

A tuple supports slicing in the same way as a list. If you want (2, 3) from (1, 2, 3, 4), that is a contiguous subtuple.

python
values = (1, 2, 3, 4)
print(values[1:3])

Output:

python
(2, 3)

To generate every contiguous subtuple, use two indices:

python
1def contiguous_subtuples(items):
2    result = []
3    for start in range(len(items)):
4        for end in range(start + 1, len(items) + 1):
5            result.append(items[start:end])
6    return result
7
8values = (1, 2, 3)
9print(contiguous_subtuples(values))

This prints:

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

Every item is a tuple because slicing a tuple returns another tuple.

Choosing Element Combinations

Sometimes subtuple means any smaller tuple made from selected elements, even when the selected values are not adjacent. That is a combinations problem, and itertools.combinations is the right tool.

python
1from itertools import combinations
2
3values = (1, 2, 3, 4)
4
5pairs = list(combinations(values, 2))
6triples = list(combinations(values, 3))
7
8print(pairs)
9print(triples)

Output:

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

Unlike slicing, combinations preserve order from the original tuple but do not require the elements to be adjacent.

Generating All Non-Empty Subtuples

If you want every non-empty combination of elements, build them length by length.

python
1from itertools import combinations
2
3
4def all_subtuples(items):
5    result = []
6    for size in range(1, len(items) + 1):
7        result.extend(combinations(items, size))
8    return result
9
10values = ("a", "b", "c")
11print(all_subtuples(values))

This returns:

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

That is different from contiguous slices because ('a', 'c') appears even though the elements are not next to each other.

Performance and Output Size

It is easy to underestimate how quickly the result grows.

For contiguous subtuples, a tuple of length n produces n * (n + 1) / 2 slices. For all combinations, the count is 2^n - 1 if you exclude the empty tuple. That growth becomes large very quickly.

If you only need to iterate instead of storing everything, prefer a generator:

python
1from itertools import combinations
2
3
4def iter_all_subtuples(items):
5    for size in range(1, len(items) + 1):
6        yield from combinations(items, size)
7
8for subtuple in iter_all_subtuples((1, 2, 3, 4)):
9    print(subtuple)

This avoids building one large list in memory.

Which Interpretation Should You Use

Use slicing when order and adjacency matter, such as extracting windows from time series or token sequences.

Use combinations when you are exploring subsets, feature groups, or possible pairings. In other words, ask whether (1, 3) should count as a valid subtuple. If the answer is yes, slicing is not enough.

You can also include the empty tuple when mathematically useful:

python
1from itertools import chain, combinations
2
3
4def powerset_as_tuples(items):
5    return chain.from_iterable(
6        combinations(items, size) for size in range(len(items) + 1)
7    )
8
9print(list(powerset_as_tuples((1, 2))))

That produces (), (1,), (2,), and (1, 2).

Common Pitfalls

The most common mistake is mixing up slices and combinations. They answer different questions even though both return tuples.

Another mistake is converting everything to a list too early. For large tuples, generating all results eagerly can use a lot of memory.

It is also easy to forget that tuple slicing uses an exclusive end index. items[1:3] includes positions 1 and 2, not 3.

Finally, if duplicates matter, remember that combinations operate on positions, not only on values. A tuple such as (1, 1, 2) can produce repeated-looking subtuples because the original positions are distinct.

Summary

  • Use slicing for contiguous subtuples.
  • Use itertools.combinations for non-contiguous element selections.
  • Decide first whether adjacency matters for your problem.
  • Prefer generators when the number of subtuples can grow large.
  • Be explicit about whether the empty tuple should be included.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.