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.
print(). In a .py file and in the editor here, wrap it in print(), as the run below does.# 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)))<class 'set'>
{1, 2, 3, 4}
['Avengers', 'Hitman', 'Hulk', 'Iron Man']
['Hulk']
['Avengers', 'Hitman', 'Iron Man']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:
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 parenthesesMaking 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.
seen = ["billing", "shipping", "billing", "other", "shipping"]
categories = set(seen)
print(len(categories))
print(sorted(categories))
print("billing" in categories)3 ['billing', 'other', 'shipping'] True
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.
monday = {"billing", "shipping", "account"}
tuesday = {"shipping", "account", "other"}
print(sorted(monday | tuesday))
print(sorted(monday & tuesday))
print(sorted(monday - tuesday))['account', 'billing', 'other', 'shipping'] ['account', 'shipping'] ['billing']
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.
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.# 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"))('Krish', 'Ankur', 'John')
Krish
1
1A 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.
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)Asha billing 1 billing 2 shipping
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.
my_tuple = ("Krish", "Ankur", "John")
my_tuple[0] = "Mike"Traceback (most recent call last):
File "main.py", line 2, in <module>
my_tuple[0] = "Mike"
TypeError: 'tuple' object does not support item assignmentA 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
| List | Set | Tuple | |
|---|---|---|---|
| Written | [1, 2, 2] | {1, 2} | (1, 2) |
| Keeps order | Yes | No | Yes |
| Allows duplicates | Yes | No | Yes |
| Can change after | Yes | Yes, add and remove | No |
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.
categories[0] is a TypeError. When you need an item by position, keep a list, or turn the set into one with sorted(categories).Related
- Previous: Dictionaries
- Next: Lists of dictionaries
- Reference: Sets in the Python tutorial
- 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.
Every expert started right here.