Python for AIPython 3.12 · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
43 small wins to finish your pathNext lesson →

Sets and tuples

A set is a collection that holds only unique values with no order, and a tuple is an ordered group of values that cannot be changed once it is made.

Last updated: 13 Oct, 2026 · Python 3.12

A dictionary gave every value a key. A set drops the keys and keeps only whether a value is present, with no repeats. A tuple keeps values in order like a list, but locks them so nothing can change them later.

Creating a set and set operations · from Tutorial 3- Python Sets, Dictionaries and Tuples · 1:00 to 4:18
The video runs each line in a Jupyter notebook, which shows the value of a cell's last line without print(). In a .py file and in the editor here, wrap it in print(), as the run below does.
ExampleFrom the video, run here
# an empty set comes from set(); braces with values make a set that drops duplicates
set_var = set()
print(type(set_var))
set_var = {1, 2, 3, 4, 3}
print(set_var)

set_var = {"Avengers", "Iron Man", "Hitman"}
set_var.add("Hulk")
print(sorted(set_var))

# a set has no order, so sort before printing; difference and intersection compare two sets
set1 = {"Avengers", "Iron Man", "Hitman"}
set2 = {"Avengers", "Iron Man", "Hitman", "Hulk"}
print(sorted(set2.difference(set1)))
print(sorted(set2.intersection(set1)))

Braces with values make a set. add puts a value in, and because a set never holds the same value twice, adding one that is already there would change nothing. union gives every value from both sets, intersection gives the values in both, and difference gives the values in the first that are not in the second. The page uses the same operations on ticket categories below.

Syntax:

python
unique = {value1, value2}      # a set, written with braces
unique = set(a_list)           # a set from a list, repeats dropped
fixed = (value1, value2)       # a tuple, written with parentheses

Making a set and dropping duplicates

Pass a list to set() and every repeated value collapses to one. This is the quickest way to count how many different things a list holds.

Example
seen = ["billing", "shipping", "billing", "other", "shipping"]
categories = set(seen)
print(len(categories))
print(sorted(categories))
print("billing" in categories)

Five labels became three. A set has no order, so sorted is used to print it in a fixed order here; the set itself is the same either way. in checks membership, and on a large set it is much faster than scanning a list.

Combining sets with union, intersection and difference

Two sets can be compared with three symbols: | for everything in either, & for what is in both, and - for what is in the first but not the second.

Example
monday = {"billing", "shipping", "account"}
tuesday = {"shipping", "account", "other"}
print(sorted(monday | tuesday))
print(sorted(monday & tuesday))
print(sorted(monday - tuesday))

monday | tuesday is the union, every category seen on either day. monday & tuesday is the intersection, the categories seen on both. monday - tuesday is the difference, billing came in on Monday only. These are the same as union, intersection and difference from the video, written as symbols.

Creating a tuple and why it is immutable · from Tutorial 3- Python Sets, Dictionaries and Tuples · 13:43 to 15:45
Same notebook note as above: wrap a line in print() to see it in a file. The video builds its tuple with parentheses, three names, and shows its only two methods, count and index.
ExampleFrom the video, run here
# a tuple is written with parentheses and reads by position, like a list
my_tuple = ("Krish", "Ankur", "John")
print(my_tuple)
print(my_tuple[0])

# a tuple has only two methods: count and index
print(my_tuple.count("Krish"))
print(my_tuple.index("Ankur"))

A tuple reads by position, the way a list does: my_tuple[0] is the first name. A tuple carries only two methods: count, how many times a value appears, and index, the position of a value.

Unpacking a tuple into names

A tuple can be split into separate names in one line, one name for each value. You saw this already in the Dictionaries lesson, where for key, value in ticket.items() unpacked each pair the loop handed over.

Example
pair = ("Asha", "billing")
name, category = pair
print(name)
print(category)

seen = [(1, "billing"), (2, "shipping")]
for ticket_id, category in seen:
    print(ticket_id, category)

name, category = pair puts the first value in name and the second in category, in order. A list of pairs unpacks the same way inside a for loop, which is how a tuple often travels: as a small fixed pair, such as an id and its category.

Locking values so they cannot change

A tuple cannot be changed after it is made. Trying to store into one of its positions stops the program, which is what makes a tuple safe for a value that must stay fixed.

Example
my_tuple = ("Krish", "Ankur", "John")
my_tuple[0] = "Mike"

A list would allow this; a tuple raises a TypeError. When a group of values should never change by accident, a tuple says so and the error catches any line that forgets.

List vs set vs tuple

ListSetTuple
Written[1, 2, 2]{1, 2}(1, 2)
Keeps orderYesNoYes
Allows duplicatesYesNoYes
Can change afterYesYes, add and removeNo

Where sets and tuples show up in AI code

  • Dropping duplicate ids or tool names with a set before a batch goes to a model.
  • A fast membership test: is this word in a set of stop words, is this id one already seen.
  • Pairing values that belong together, such as an id and its category, without a dictionary.
  • Holding a fixed record that must not change, such as a coordinate or a row of settings.
Watch out. A set has no order and no positions, so categories[0] is a TypeError. When you need an item by position, keep a list, or turn the set into one with sorted(categories).
Try it yourself
  • Make a set from ["a", "b", "a", "c"] and print its length.
  • Build two sets of tags and print the ones in both with &.
  • Change the first item of a tuple and read the error you get.
PreviousDictionaries

Every expert started right here.