Home / Blog / Programming / Python
Python List vs Tuple: When to Use Each
Programming · Python · 7 min read
Lists change after creation and tuples do not, and that single difference decides which one fits a given collection.
Two brackets, two promises
A list and a tuple both hold a sequence of items in order. A sequence is a collection where position matters, so the first item is not the same as the second. Both kinds can hold words, numbers, or a mix of the two, and both support reading an item by its position.
A list is written in square brackets. A list is mutable, which means its contents can be changed after creation. Items can be added, removed, or replaced, and the list itself stays the same object throughout.
A tuple is written in round brackets. A tuple is immutable, which means its contents cannot be changed after creation. An attempt to replace one item raises an error, and the tuple must be replaced as a whole instead.
That one difference drives almost every choice between them. A list promises room to change. A tuple promises that nothing will change. The rest of the choice is deciding which promise a given collection needs.
basket = ["apples", "plums", "pears"] point = (4, 7) print(basket) print(point)
When the collection will change, use a list
Use a list when items arrive over time. A shopping basket gains items, a queue of messages grows and shrinks, and a set of search results is rebuilt with each new search. Each of these collections is a pile of similar things, and its size is not known in advance.
Lists carry the tools for that work. The append method adds one item at the end. An item at a known position can be replaced by assignment. Items can also be removed, and the whole list can be sorted in place when a new order is needed.
This flexibility has a cost in intent, not in speed. A reader who sees a list cannot tell whether it is meant to stay as it is, or whether later code will change it. That ambiguity is fine for a working collection, because change is exactly what the collection is for.
A good test is to ask whether the collection would still make sense with one more item or one fewer. If the answer is yes, the collection is a growing pile, and a list is the natural home for it.
basket = ["apples", "plums"]
basket.append("pears")
basket[0] = "peaches"
print(basket)When the record is fixed, use a tuple
Use a tuple when the items form one fixed record. A point on a grid has exactly two numbers, and neither number is optional. A colour has exactly three numbers, one for each channel. Removing one part would break the meaning of the whole.
A fixed record is different from a pile. A pile holds many items of the same kind, and its length varies. A record holds a small set of parts that belong together, and each part has its own role. The first number of a grid point is not interchangeable with the second.
Immutability protects a record from quiet damage. Code elsewhere cannot replace one part by accident and leave the rest looking valid. When a record must change, the honest move is to build a new tuple with the new value in place, so the change is visible at the point where it happens.
Functions use this shape constantly. A function that returns two values returns a single tuple holding both, and the caller unpacks it. Unpacking means assigning each part of a tuple to its own name in one step, so both values arrive clearly labelled.
point = (4, 7) colour = (255, 128, 0) print(point) print(colour)
Why only a tuple works as a dictionary key
A dictionary key must be hashable. Hashable means Python can compute a fixed fingerprint for the value, and use that fingerprint to find it again later. If the value changed after the fingerprint was taken, the fingerprint would no longer match, and the lookup would fail.
A list cannot be a dictionary key, for exactly that reason. A list can change, so its fingerprint could go stale at any moment. Python refuses a list key with an error, rather than allowing a lookup that might silently break.
A tuple can be a dictionary key, provided everything inside it is hashable too. Its contents cannot change, so its fingerprint stays valid for its whole life. Grid points, dates, and pairs of labels all make natural tuple keys for this reason.
The same rule governs sets. A set is a collection of unique values, and it finds its members by fingerprint. Tuples can be set members, while lists cannot, and the reason is the same fingerprint rule.
shelf = {(4, 7): "second row"}
print(shelf[(4, 7)])Two traps that catch beginners
The first trap is the single-item tuple. Round brackets alone do not make a tuple, because brackets are also used for grouping. The comma makes the tuple, not the brackets, so a one-item tuple is written with a trailing comma. Without the comma, the value is just the item itself, not a tuple at all.
The second trap is subtler. A tuple is immutable, but immutability only covers the tuple itself. The tuple holds references to its items, and a reference is a pointer to an object stored elsewhere. If one of those objects is a list, that list can still change, even though the tuple still holds the same reference.
That is why a tuple containing a list cannot be a dictionary key. The fingerprint rule looks through the tuple at its contents, and a changeable list inside breaks the promise of a fixed fingerprint. A tuple is only fully fixed when everything inside it is fixed too.
Both traps have the same lesson. Immutability is a promise about the container, and it holds only as far as the contents allow. Checking what sits inside a tuple matters as much as choosing the tuple in the first place.
single = (5,) print(type(single)) box = ([1, 2], 3) box[0].append(9) print(box)
Choose by one question, and convert when needed
The choice comes down to one question. Will this collection change after it is made? If the answer is yes, use a list. If the answer is no, use a tuple. Most real cases answer that question at a glance, once the collection is described honestly.
Speed should not overturn that answer. Tuples can be slightly smaller and quicker to create, because Python knows their size will never change. The difference is small, and it matters only in code that builds enormous numbers of short-lived collections. Readability and safety matter far more in ordinary code.
Conversion covers the middle cases. The list function turns a tuple into a new list, and the tuple function turns a list into a new tuple. Neither conversion changes the original collection, and both copy the references to the same items, not the items themselves.
A list that has finished changing can be frozen into a tuple for safe keeping. A tuple that must gain a new stage can be converted, changed, and converted back. The conversion is a visible step, and that visibility is a feature. It marks the exact moment a fixed record became a working collection, or the reverse.
readings = (12, 18, 21) print(list(readings)) print(tuple([12, 18, 21]))
Questions readers ask
Can I change a tuple by converting it to a list?
Yes, but the conversion makes a new list. The tuple itself never changes. Change the list, and convert back to a tuple only if a fixed version is needed again.
Is a tuple always faster than a list?
Tuples can be slightly smaller and quicker to create, but the gap is small and rarely decides anything. Choose by whether the collection must change, not by speed.
Can a tuple hold different kinds of data?
Yes. Both can hold mixed kinds of data, such as a word beside a number. Mixing kinds is fine, but a record with named parts is often clearer than a plain tuple.
What does a function return when it returns two values?
A function that returns two values returns one tuple holding both. The two names on the left of the assignment each receive one part of that tuple.