Python gives us three core collection types — lists, tuples, and sets. They look similar but behave very differently. Picking the right one matters.
List [ ]
Ordered: Yes
Mutable: Yes
Duplicates: Yes
Hashable: No
Best for: ordered, changeable data
Tuple ( )
Ordered: Yes
Mutable: No
Duplicates: Yes
Hashable: Yes*
Best for: fixed data, dict keys
Set { }
Ordered: No
Mutable: Yes
Duplicates: No
Hashable: No
Best for: unique items, fast lookup
*Tuples are hashable only if all their elements are hashable
Lists
Lists are the workhorse of Python. Ordered, mutable, and can hold mixed types.
fruits = ["apple", "banana", "cherry"]
fruits.append("date") # add to end
fruits.insert(1, "avocado") # insert at index 1
fruits.extend(["fig", "grape"]) # add multiple items
fruits.pop() # remove and return last item
fruits.pop(0) # remove and return item at index 0
fruits.remove("banana") # remove first occurrence by value
fruits.sort() # sort in place
fruits.reverse() # reverse in place
Tuples
Tuples are like lists that can’t be changed. Once created, we can’t add, remove, or modify elements.
point = (3, 4)
colors = ("red", "green", "blue")
single = (42,) # note the comma — without it, (42) is just an int
# Tuple unpacking — super useful
x, y = point # x = 3, y = 4
a, *rest = (1, 2, 3, 4) # a = 1, rest = [2, 3, 4]
# Swapping variables — classic Python trick
a, b = b, a
Since tuples are immutable (and hashable), we can use them as dictionary keys. Lists can’t do that.
Sets
Sets are unordered collections of unique elements. They’re blazing fast for membership checks (in operator) because they use hash tables internally.
nums = {1, 2, 3, 3, 3} # {1, 2, 3} — duplicates removed
nums.add(4) # add single item
nums.discard(2) # remove (no error if missing)
nums.remove(1) # remove (raises KeyError if missing)
# Set operations — these come up in interviews a lot
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a | b # union: {1, 2, 3, 4, 5, 6}
a & b # intersection: {3, 4}
a - b # difference: {1, 2}
a ^ b # symmetric difference: {1, 2, 5, 6}
Quick Rule of Thumb
- Need to change items frequently? Use a list.
- Data should never change? Use a tuple.
- Need unique items or fast lookups? Use a set.
In simple language, lists are flexible notebooks, tuples are printed receipts, and sets are collections of unique stamps — no duplicates allowed.