Tuples
Python - A Quick Start for existing Programers
3 min read
Published Sep 16 2025, updated Sep 30 2025
Guide Sections
Guide Comments
A tuple in Python is:
- Ordered: elements have a fixed order and can be indexed.
- Immutable: once created, elements cannot be added, removed, or changed.
- Iterable: can be looped over, unpacked, sliced.
- Heterogeneous: can hold different data types.
- Performance: Tuples are slightly faster than lists for iteration and access, since they’re fixed-size and don’t need dynamic resizing so they are a better choice than lists for large data that won't change.
- Less memory: Tuples use less memory than lists so are useful when using large fixed-size data.
(): Tuples are defined using()standard brackets.
Examples:
Python tuples are indexed collections, however you can also use negative indexing:
- Zero-based indexing → the first element is at index
0, the second at1, and so on. - Negative indexing → counts from the end:
-1is the last element,-2the second-to-last, etc.
Examples:
Tuple unpacking
Tuple unpacking allows you to assign each element of a tuple to a separate variable in a single statement.
Rules:
- Number of variables must match the number of elements in the tuple (unless using
*for extended unpacking). - Parentheses are optional in many cases:
a, b, c = 1, 2, 3works too. - Supports nested unpacking for tuples inside tuples.
- Can be used in for loops, function returns, and multiple assignments.
Examples:
Built-in functions used with tuples
Since tuples are iterable, all the common built-ins for iterables work:
len(tuple)→ number of elementsmin(tuple),max(tuple)→ smallest/largest elementsum(tuple)→ sum of elements (if numeric)sorted(tuple)→ returns a new list sorted from the tupleenumerate(tuple)→ index-value pairsany(tuple),all(tuple)→ logical checkstuple(iterable)→ casting from iterable to tuple
Examples:
Tuples own functions
Tuples are immutable, so they only have two methods:
Function | Description |
| Return number of occurrences of |
| Return the first index of |
Examples:
Casting to a tuple in
You can call tuple(x) to create a tuple from any iterable (something you can loop over).
Works with:
- Strings → breaks into a tuple of characters
- Lists → converts into tuple of elements
- Sets → unordered elements become a tuple
- Dictionaries → by default, only keys are taken
- Range objects → numbers expanded into a tuple
- Other iterables (generators, iterators, files, custom classes that implement
__iter__)
Does not work with non-iterables (like int, float, None) → raises TypeError.
Examples:
Tuple Comparisons
Python compares tuples lexicographically (element by element, like words in a dictionary).
Rules:
- Compare the first elements of each tuple.
- If the first elements are equal, compare the next elements, and so on.
- The first pair of elements that differ decides the comparison.
- If all compared elements are equal but one tuple is shorter, the shorter tuple is considered smaller.
- Elements must be comparable types (e.g., numbers with numbers, strings with strings). Otherwise, a
TypeErroroccurs.
Supported operators:
==,!=→ equality / inequality<,<=,>,>=→ lexicographical ordering
Examples:














