Three containers, one recurring question: when should you use list, when tuple, and when set? Picking the right one often means writing far less code.
| Comparison | list | tuple | set |
|---|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) | {1, 2, 3} |
| Ordered | Yes | Yes | No guarantee |
| Mutable | Yes | No | Yes |
| Can add items | Yes | No | Yes |
| Can remove items | Yes | No | Yes |
| Allows duplicates | Yes | Yes | No |
Indexing [0] | Yes | Yes | No |
Slicing [1:3] | Yes | Yes | No |
| Mixed types | Yes | Yes | Yes (must be hashable) |
| Typical use | Dynamic collections | Fixed records | Dedup, set ops |
Aset's elements must be hashable: integers, strings, and tuples whose contents are themselves hashable. You can't put alistordictdirectly into a set.
files = ["boot.img", "system.img"]
files.append("vendor.img") # add
files[0] = "boot_new.img" # modify
files.remove("system.img") # remove first match
files.sort() # in-place sort
print(files[0])
print(len(files))
| Method | Purpose |
|---|---|
append(x) | Add one item to the end |
extend(iterable) | Append all items from an iterable |
insert(i, x) | Insert at a given position |
remove(x) | Remove first match; raises if not found |
pop(i) | Remove and return item; defaults to last |
sort() | In-place sort |
reverse() | In-place reverse |
index(x) | First index of a value |
count(x) | Number of occurrences |
clear() | Remove all items |
position = (100, 200)
print(position[0]) # 100
x, y = position # unpack
print(x, y) # 100 200
a = (10) # int, not a tuple
b = (10,) # tuple — the comma matters
This is a classic gotcha: (10) is just a parenthesized integer. You need (10,) to make a tuple.
data = (1, 2, 2, 3)
print(data[0]) # 1
print(data.count(2)) # 2
print(data.index(3)) # 3
print(len(data)) # 4
A tuple can't be modified, but you can read it, index it, slice it, and use count() and index().
partitions = {"boot", "system", "boot"}
print(partitions) # {'boot', 'system'} — order not guaranteed
| Method | Purpose |
|---|---|
add(x) | Add one element |
update(iterable) | Add multiple elements |
remove(x) | Remove; raises if missing |
discard(x) | Remove; silent if missing |
pop() | Remove and return an arbitrary element |
clear() | Remove all elements |
The remove() vs discard() difference is worth remembering: when you're not sure the element exists, discard() saves you an explicit check.
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # union: {1, 2, 3, 4, 5}
print(a & b) # intersection: {3}
print(a - b) # difference: {1, 2}
print(a ^ b) # symmetric: {1, 2, 4, 5}
partitions = {"boot", "system", "vendor"}
if "boot" in partitions:
print("found boot partition")
To create an empty set, useset()— because{}creates an emptydict.
empty_set = set()
empty_dict = {}
| Scenario | Use | Why |
|---|---|---|
| A list of file paths | list | Add, remove, sort, preserve order |
| A fixed coordinate (x, y) | tuple | Fixed structure, unpackable |
| Unique partition names | set | Automatic dedup |
| Run tasks in order | list | Order is preserved |
| Diff two file lists | set | Difference and intersection |
| Return multiple fixed results | tuple | Return and unpack in one go |
The instinct is to use set(), but it doesn't preserve order:
files = ["boot.img", "system.img", "boot.img"]
print(set(files))
When order matters, use dict.fromkeys():
unique_files = list(dict.fromkeys(files))
print(unique_files)
# ['boot.img', 'system.img']
a = [1, 2, 3]
b = tuple(a)
c = list(b)
print(b) # (1, 2, 3)
print(c) # [1, 2, 3]
list(), tuple(), and set() convert any iterable into the target type. Converting to a set drops duplicates and gives up order guarantees.
data = ([1, 2], 3)
data[0].append(4) # legal
print(data) # ([1, 2, 4], 3)
A tuple locks the references, not the objects they point to. This is one of the easiest things to misread.
listtuplesetlist is the default; switch to tuple for fixed structure, and set for dedup or set operations.(10) is an int; (10,) is a tuple.{} is an empty dict; write set() for an empty set.list(dict.fromkeys(x)).set's elements must be hashable — no list or dict inside.