Academy

Tuples & Sets

Not Everything Should Be a List

Lists are flexible and mutable — you can add, remove, and reorder items freely. That flexibility is powerful. But it also means a list offers no guarantee about its contents. Anyone can change it at any time.

Python gives you two other collection types, each designed for a specific situation where a list is the wrong tool.


Tuples — Ordered and Unchangeable

A tuple looks like a list but uses parentheses instead of square brackets:

coordinates = (28.6, 77.2)

Indexing works identically to lists. The difference is what you cannot do:

TypeError. Tuples are immutable — once created, they cannot be modified. No appending, no replacing, no removing.

Why Would You Want That?

Immutability is a feature when the data should not change. Geographic coordinates don't change. The RGB values of a colour don't change. The dimensions of a standard page don't change. A tuple communicates to everyone reading your code: "This is fixed. Don't try to modify it."

A list promises nothing about stability. A tuple promises permanence. That distinction is meaningful.

Tuple Packing and Unpacking

You can assign a tuple's values directly into separate variables in one line:

This is called unpacking. Python matches positions left to right. The number of variables must match the number of items in the tuple.

Unpacking works with any iterable, but it's most commonly used with tuples because tuples are naturally fixed-length:

Functions Returning Multiple Values

Python functions can only return one thing. But that one thing can be a tuple — which effectively lets you return multiple values at once.

return min(numbers), max(numbers) packs two values into a tuple automatically. The caller unpacks them in the same line. This is an extremely common Python pattern.


Sets — Unique Items Only

A set is a collection where every item appears exactly once. Duplicates are eliminated automatically.

Six items in, but only four unique ones come out. Notice also that the order is not preserved — sets are unordered. You cannot access items by index.

Sets are created with curly braces {} — but an empty set must be created with set(), not {} (which creates an empty dictionary):

empty_set = set()

The Most Common Use: Removing Duplicates

Convert to a set to eliminate duplicates, then back to a list if you need order or indexing. Two conversions, one purpose.

Membership Testing

Sets are significantly faster than lists for checking whether an item exists — especially with large collections. The in operator works the same way:

For small collections the difference is imperceptible. For tens of thousands of items, sets are orders of magnitude faster than lists.

Set Operations

Sets support the mathematical operations you may remember from school:

  • & — intersection: items in both sets
  • | — union: items in either set
  • - — difference: items in the first but not the second

These operations are useful whenever you compare groups of data.


Choosing the Right Collection

SituationUse
Ordered, changeable collectionlist
Fixed, unchangeable sequencetuple
Unique items, fast membership checkset
Labelled data (key → value)dict

The right choice is not always obvious at first, but the question to ask is: does order matter? Can it change? Does uniqueness matter? The answers point to the right type.

What You Have Learned

Tuples and sets expand your toolkit beyond lists — each with a specific strength.

The key ideas:

  • Tuples are immutable ordered sequences — use them for data that should not change
  • Tuple unpacking assigns multiple values in one line: a, b = (1, 2)
  • Functions returning multiple values implicitly return a tuple
  • Sets store unique items only — duplicates are silently removed
  • Sets are unordered — no indexing, no guaranteed sequence
  • in on a set is much faster than in on a list for large collections
  • Set operations: & (intersection), | (union), - (difference)

In the next lesson, you'll go deeper into functions — learning how to write functions that accept any number of arguments and how to pass functions as values to other functions.