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.
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.
Output:
To generate every contiguous subtuple, use two indices:
This prints:
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.
Output:
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.
This returns:
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:
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:
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.combinationsfor 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

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 courseTrack 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.