← Back to Home

Python list, tuple, set: How to Choose the Right Container

2026.10.15 · Python 7 min read

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.

Side-by-side comparison

Comparisonlisttupleset
Syntax[1, 2, 3](1, 2, 3){1, 2, 3}
OrderedYesYesNo guarantee
MutableYesNoYes
Can add itemsYesNoYes
Can remove itemsYesNoYes
Allows duplicatesYesYesNo
Indexing [0]YesYesNo
Slicing [1:3]YesYesNo
Mixed typesYesYesYes (must be hashable)
Typical useDynamic collectionsFixed recordsDedup, set ops
A set's elements must be hashable: integers, strings, and tuples whose contents are themselves hashable. You can't put a list or dict directly into a set.

list: add, modify, remove, sort

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))

Common methods

MethodPurpose
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

tuple: fixed data and unpacking

position = (100, 200)

print(position[0])   # 100

x, y = position      # unpack
print(x, y)          # 100 200

Single-element tuple

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.

What you can still do

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().

set: dedup, lookup, set operations

Duplicates removed automatically

partitions = {"boot", "system", "boot"}
print(partitions)   # {'boot', 'system'} — order not guaranteed

Common methods

MethodPurpose
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.

Set operations

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}

Membership check

partitions = {"boot", "system", "vendor"}

if "boot" in partitions:
    print("found boot partition")
To create an empty set, use set() — because {} creates an empty dict.
empty_set = set()
empty_dict = {}

Real-world use cases

ScenarioUseWhy
A list of file pathslistAdd, remove, sort, preserve order
A fixed coordinate (x, y)tupleFixed structure, unpackable
Unique partition namessetAutomatic dedup
Run tasks in orderlistOrder is preserved
Diff two file listssetDifference and intersection
Return multiple fixed resultstupleReturn and unpack in one go

Practical techniques

1. Dedup while keeping order

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']

2. The three types convert freely

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.

3. A tuple is immutable, but its contents might not be

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.

Quick memory aid

Key takeaways