Working with DataExtra· 35 min read

Tuples & Sets

Two more built-in collections — tuples that never change, and sets that hold only unique items.

What you will learn

  • Use tuples for fixed groups of values
  • Unpack a tuple into separate variables
  • Use sets for uniqueness and set operations

Tuples: a list that cannot change

A tuple is an ordered group of values, just like a list — but with one big difference: once you make it, you cannot change it. You cannot add, remove, or replace items. This “cannot be changed” quality has a name: immutable. You write a tuple with round brackets ( ) instead of square ones.

Why would you ever want a collection you cannot edit? Because some groups of values should stay fixed — a point on a map (latitude, longitude), the red-green-blue parts of a colour, or a row from a database. Making them tuples protects them from being changed by accident.

A tuple uses round brackets
point = (4, 9)
print(point[0])        # 4  (read by index, just like a list)
print(len(point))      # 2

colour = (255, 128, 0)
print("Red part:", colour[0])

Line by line: point = (4, 9) makes a tuple of two numbers. You read items exactly like a list — point[0] reaches index 0 and gives 4 — and len() counts them, here 2. The colour tuple holds the three parts of an orange colour, and colour[0] reads the red part, 255. Reading works just like a list; the only thing you cannot do is change it.

Note: Output: 4 2 Red part: 255

Watch out: Trying to change a tuple raises an error. point[0] = 5 fails with TypeError: 'tuple' object does not support item assignment — that is the immutability protecting you.

Unpacking: pull a tuple apart

A very handy trick is unpacking — assigning a tuple to several variables at once, so each value lands in its own name. It is clean and reads beautifully.

Unpacking a tuple into variables
point = (4, 9)
x, y = point          # unpacking
print("x is", x)
print("y is", y)

The line x, y = point takes the two values inside point and hands the first (4) to x and the second (9) to y in one move. Now you can use x and y as ordinary variables. The number of names on the left must match the number of items in the tuple.

Note: Output: x is 4 y is 9

Tip: You already met unpacking without knowing it: for key, value in user.items(): unpacks each pair into two variables. Functions can also return several values as a tuple, which you then unpack.

Sets: a bag of unique items

A set is a collection that automatically keeps only unique items — it throws away duplicates for you. It also does not keep items in any particular order, so you cannot read a set by index. You write one with curly braces { } (the same braces as a dictionary, but with single values, not key–value pairs).

A concrete example: imagine a sign-up list where the same email got entered twice. Turning the list into a set instantly removes the repeat.

set() removes duplicates
emails = ["a@x.com", "b@x.com", "a@x.com"]
unique = set(emails)
print(unique)            # duplicate gone
print(len(unique))       # 2

fruits = {"apple", "mango"}
fruits.add("apple")      # already there — ignored
print(len(fruits))       # still 2

Step by step: set(emails) turns the list into a set, and because "a@x.com" appeared twice, the set keeps just one copy — so len(unique) is 2, not 3. With fruits, calling .add("apple") when apple is already inside changes nothing — a set never stores the same value twice. (The order things print may differ on your computer, since sets are unordered.)

Note: Output: {'a@x.com', 'b@x.com'} 2 2

Set operations: compare two groups

Sets shine when you want to compare two groups — find what they share, or what is in one but not the other. These mirror the maths idea of sets you may have met in school.

Compare two sets
maths = {"Asha", "Ravi", "Sara"}
science = {"Ravi", "Sara", "Kiran"}

print(maths & science)   # in BOTH (intersection)
print(maths | science)   # in EITHER (union)
print(maths - science)   # in maths only (difference)

Reading each line: & gives the intersection — students in both classes (Ravi, Sara). | gives the union — everyone in either class, with no repeats. - gives the difference — students in maths but not science (just Asha). These three operators answer real questions like “who is enrolled in both?” in one line.

Note: Output: {'Ravi', 'Sara'} {'Asha', 'Ravi', 'Sara', 'Kiran'} {'Asha'}

Here is a quick way to remember when to reach for each of the four collections:

CollectionBracketsChangeable?Use it for
list[ ]YesAn ordered list you will edit
tuple( )No (immutable)A fixed group that must not change
set{ }YesUnique items, comparing groups
dict{key: val}YesNamed key–value details

Q. What is the key difference between a tuple and a list?

Answer: A tuple is immutable — once created you cannot add, remove or change its items. A list can be edited freely.

✍️ Practice

  1. Make a tuple of (latitude, longitude) and unpack it into two variables.
  2. Turn a list with duplicates into a set and print how many unique items remain.

🏠 Homework

  1. Given two sets of skills (yours and a friend’s), print the skills you share and the skills only you have.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →