Aral Code

Collections Lesson 16 of 23

Python Tuples and Sets

Use tuples for values that shouldn't change, unpack them into variables, and use sets to remove duplicates and compare groups with union and intersection.

Lists are the most common collection, but Python has two more that solve special problems: tuples for data that should never change, and sets for collections with no duplicates.

Tuples

A tuple is like a list, but immutable: once created, it can't be changed. Tuples use parentheses:

manila = (14.5995, 120.9842)          # latitude, longitude
birthday = (2011, 3, 14)               # year, month, day
rgb_red = (206, 17, 38)

print(manila[0])
print(len(birthday))

Output

14.5995
3
manila[0] = 15

Error

TypeError: 'tuple' object does not support item assignment

Why use tuples?

  • Safety: data like coordinates, dates or RGB colors shouldn't change by accident.
  • Meaning: a tuple says "these values belong together as one record".
  • Dictionary keys: tuples can be used as keys in dictionaries; lists can't.
  • Returning several values from a function (you'll see this in Functions).

Unpacking

You can split a tuple into separate variables in one line:

student = ("Maria", 10, "Rizal")
name, grade, section = student
print(f"{name} is in Grade {grade} - {section}")

Output

Maria is in Grade 10 - Rizal

The number of variables must match the number of items, or Python raises a ValueError.

One-item tuples

a = (5)     # just the number 5 in parentheses
b = (5,)    # a tuple with one item
print(type(a), type(b))

Output

<class 'int'> <class 'tuple'>

The trailing comma is what makes it a tuple.

Sets

A set is an unordered collection with no duplicates. Sets use curly braces:

colors = {"red", "blue", "red", "yellow", "blue"}
print(colors)
print(len(colors))

Output

{'yellow', 'red', 'blue'}
3

The duplicates disappear automatically. The order may look random, because sets don't keep order.

Removing duplicates from a list

votes = ["Ana", "Ben", "Ana", "Carla", "Ben", "Ana"]
candidates = set(votes)
print("Candidates who got votes:", sorted(candidates))
print("Number of candidates:", len(candidates))

Output

Candidates who got votes: ['Ana', 'Ben', 'Carla']
Number of candidates: 3

Adding, removing and checking

attended = {"Ana", "Ben"}
attended.add("Carla")
attended.add("Ana")         # already there, nothing happens
attended.discard("Ben")     # remove (no error if missing)
print("Ana" in attended)
print(sorted(attended))

Output

True
['Ana', 'Carla']

Checking in is extremely fast with sets, even with millions of items. That's another reason to use them.

Set operations

Sets can be compared like circles in a Venn diagram:

basketball = {"Ana", "Ben", "Dan", "Ela"}
chess = {"Ben", "Carla", "Ela", "Fe"}

print("Both clubs:", basketball & chess)
print("Either club:", basketball | chess)
print("Basketball only:", basketball - chess)
print("Exactly one club:", basketball ^ chess)

Output

Both clubs: {'Ben', 'Ela'}
Either club: {'Ana', 'Ben', 'Carla', 'Dan', 'Ela', 'Fe'}
Basketball only: {'Ana', 'Dan'}
Exactly one club: {'Ana', 'Carla', 'Dan', 'Fe'}
OperatorMethodMeaning
&intersection()In both
|union()In either
-difference()In the first but not the second
^symmetric_difference()In exactly one

Which collection should I use?

CollectionOrdered?Changeable?Duplicates?Use for
list [ ]YesYesYesMost lists of things
tuple ( )YesNoYesFixed records, coordinates
set { }NoYesNoUnique items, fast lookup
dict {k: v}YesYesKeys: noLabeled data (next lesson)

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What is the main difference between a list and a tuple?
2. What does set([1, 1, 2, 3, 3]) produce?
3. How do you create an empty set?
4. What does {1, 2, 3} & {2, 3, 4} give?

By Sir Reyel · Last updated 2026-12-04