ShareYourGroupShareYourGroupPython

Lesson 13 of 21 · about 6 minutes with the exercises

Tuples and sets

Lists are the everyday collection, but Python has two close relatives that are better for certain jobs. A tuple is a list that cannot change. A set is a bag of unique items with no order.

Tuples

Write a tuple with round brackets.

point = (3, 5)
date = (12, 11, 2026)
print(point[0])
print(len(date))
print(date)

Output

3
3
(12, 11, 2026)

Reading works like a list: indexes, slices, len, loops and in. What you cannot do is change it.

date = (12, 11, 2026)
date[0] = 13

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    date[0] = 13
    ~~~~^^^
TypeError: 'tuple' object does not support item assignment

Why use something that cannot change?

Unpacking

You can spread a tuple into separate variables in one line. The number of names must match the number of items.

date = (12, 11, 2026)
day, month, year = date
print(month)
print(f"{day:02d}/{month:02d}/{year}")

Output

11
12/11/2026

You have already used this. x, y = y, x builds a tuple on the right and unpacks it on the left. for name, mark in zip(names, marks) unpacks a pair on every round.

A tuple with one item

A single value in brackets is just that value. To make a tuple of one, add a comma.

a = (5)
b = (5,)
print(type(a))
print(type(b))

Output

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

A list of tuples

A very common shape for data is a list where each item is a small tuple, like the rows of a table.

students = [("Asha", 78), ("Ravi", 91), ("Meena", 66)]
for name, marks in students:
    print(name, marks)
best = max(students, key=lambda s: s[1])
print("Topper:", best[0])

Output

Asha 78
Ravi 91
Meena 66
Topper: Ravi

The key tells max to compare by the second part of each tuple, the marks. The word lambda makes a tiny one-line function. You will not need to write your own for a while.

Sets

Write a set with curly brackets. A set keeps only one copy of each value.

colours = {"red", "green", "red", "blue", "green"}
print(len(colours))
print("red" in colours)

Output

3
True

Five values went in and three stayed. A set has no order and no indexes, so colours[0] is an error, and printing a whole set may show the items in any order.

Removing duplicates from a list

This is the most common use of a set. Turn the list into a set and back. Sorting at the end gives a steady order.

votes = ["tea", "coffee", "tea", "lassi", "tea", "coffee"]
unique = sorted(set(votes))
print(unique)
print(len(set(votes)), "different drinks")

Output

['coffee', 'lassi', 'tea']
3 different drinks

Adding and removing

seen = set()
seen.add("Asha")
seen.add("Ravi")
seen.add("Asha")
print(len(seen))
seen.discard("Ravi")
print(seen)

Output

2
{'Asha'}

Note that an empty set is made with set(). Empty curly brackets {} make an empty dictionary, which is the next lesson.

Comparing two groups

Sets can answer questions about two groups in one step.

cricket = {"Asha", "Ravi", "Kabir"}
chess = {"Ravi", "Meena", "Kabir"}
print(sorted(cricket & chess))
print(sorted(cricket | chess))
print(sorted(cricket - chess))

Output

['Kabir', 'Ravi']
['Asha', 'Kabir', 'Meena', 'Ravi']
['Asha']
SignNameMeaning
&intersectionin both
|unionin either
-differencein the first but not the second

The examples use sorted only so that the printed order is the same every time.

Fast membership tests

Asking x in a_set is very quick even when the set holds a million items, while a list must be searched from the start. When a program checks membership again and again, a set is the right choice.

Which one should I use?

NeedUse
Items in order that will changelist
A fixed group of related valuestuple
Unique items, order not importantset
Look up a value by a namedictionary (next lesson)

Try it yourself

Exercise 1. Store a time as the tuple (9, 30). Unpack it into hour and minute and print it as 09:30.

Show one answer
time = (9, 30)
hour, minute = time
print(f"{hour:02d}:{minute:02d}")

Output

09:30

Exercise 2. How many different letters are in the word "mississippi"? Print them in alphabetical order too.

Show one answer
letters = set("mississippi")
print(len(letters))
print(sorted(letters))

Output

4
['i', 'm', 'p', 's']

Exercise 3. Monday's visitors were ["Asha", "Ravi", "Tara"] and Tuesday's were ["Ravi", "Joy", "Asha"]. Print who came on both days.

Show one answer
monday = set(["Asha", "Ravi", "Tara"])
tuesday = set(["Ravi", "Joy", "Asha"])
print(sorted(monday & tuesday))

Output

['Asha', 'Ravi']

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.