What is a Set?

Concepts covered: Sets

A set holds each value at most once and gives those values no positions. You put values in, you ask whether a value is present, and you count how many distinct values you hold. A list can store the same ticket number 5 times; a set given the same 5 numbers keeps 1 copy of each and drops the rest on its own, with no code from you to check for repeats.

scanned_list = [4521, 4522, 4521, 4523, 4521]
scanned_set = {4521, 4522, 4521, 4523, 4521}
print(scanned_list)
print(scanned_set)
print(len(scanned_list), "scans")
print(len(scanned_set), "distinct tickets")

Output:

[4521, 4522, 4521, 4523, 4521]
{4521, 4522, 4523}
5 scans
3 distinct tickets

Both collections received the same 5 numbers. The list reports 5 scans because it recorded every arrival. The set reports 3 distinct tickets because the second and third arrivals of 4521 found it already present and changed nothing. Printing the set shows braces, which also appear around dictionaries, so look for the missing colons: a set has values only, with no key pointing at any of them.

No positions, so no indexing

A list remembers where each item sits, so scanned_list[0] is a meaningful request: the first thing scanned. A set remembers only which values are present. There's no first element, so asking for [0] on a set raises an error. The order Python prints a set in comes from its internal storage layout, and your code must never lean on it.

colors = {"red", "green", "blue"}
print(colors[0])

Subscriptable means 'accepts square brackets after it'. A list is subscriptable, as are strings and dictionaries. A set is not.

Membership in 1 step

The in operator works on both, but the work behind it differs. A list compares your value against item after item from the front until one matches or the list ends. A set computes a slot from the value itself and looks in that 1 slot. At 10 items you won't notice a difference. At 10 million, the list might make 10 million comparisons before saying False, while the set still takes a handful of steps.

allowed_ids = {17, 23, 42}
print(23 in allowed_ids)
print(24 in allowed_ids)
print(24 not in allowed_ids)

Output:

True
False
True

Every answer here matches what a list of the same 3 ids would give. The difference is cost, and cost is why the structure exists: a program that tests membership thousands of times a second needs the 1-slot version.

Side by side

List
  • Keeps items in the order you added them
  • Stores the same value as many times as you add it
  • Reaches an item by position: items[0]
  • in compares against items one by one
Set
  • Has no order you can rely on
  • Keeps 1 copy of each value, always
  • No positions, so square brackets raise an error
  • in jumps straight to the value's slot
TaskListSet
Hold 7 three times`[7, 7, 7]` holds 3 items`{7, 7, 7}` holds 1 item
Get the first item`items[0]`No such thing
Count what is stored`len` counts every copy`len` counts distinct values
Print itSquare brackets, insertion orderBraces, storage order

When to pick a set over a list

What does this program print?

a = {1, 2, 2, 3, 3, 3}
b = {3, 2, 1}
print(len(a), a == b)

That question leaned on 2 facts: repeats collapse when the set is built, and len counts what survived. Add in and you have the pattern behind most everyday set work: write the raw values as a set literal, let the repeats fall away, then ask len for the distinct count and in about any single value. Here a door scanner logged 6 badge swipes from 4 different people.

swipes = {301, 302, 301, 305, 302, 309}
print(len(swipes))
print(305 in swipes)
print(310 in swipes)

This prints 4, then True, then False. Ids 301 and 302 each arrived twice, so the 6 ids in the literal shrink to 4 members. A list of the same ids would report 6 from len, which counts swipes rather than people, and that gap is the reason to choose the set here.

About this lesson

This section is part of the Sets: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.