When we work with collections of data in Python, we have several options available. Two of the most fundamental data structures we encounter are lists and tuples. While they appear similar at first glance, they have important differences that make each one suitable for different situations.
What Makes Lists and Tuples Different?
The core difference between lists and tuples relates to a concept called mutability.
A list is mutable, which means we can add, remove, or modify elements after the list has been created.
A tuple is immutable, which means once we create it, we cannot change it. The data stays exactly as we set it up. If we try to modify a tuple, Python will stop us with an error.
When Should We Use Lists?
We should use a list when we need a collection that will change over time. Lists are perfect for situations where we add, remove, or update information.
A practical example is a to-do list application. We start with some tasks, and throughout the day we add new tasks, mark some as complete (and remove them), or change task descriptions.
# List - mutable
tasks = ['write code', 'review PR']
tasks.append('deploy') # Works fine
tasks[0] = 'refactor code' # Can modify
print(tasks) # ['refactor code', 'review PR', 'deploy']
When Should We Use Tuples?
We should use a tuple for data that should not change. Tuples are perfect for fixed data that represents something that should stay constant throughout our program.
Tuples have two additional advantages. First, tuples are slightly faster than lists because they are immutable. Python does not need to manage changes or reindex elements when we add or remove items. Second, tuples serve as a clear signal to other developers reading our code. When someone sees a tuple, they understand that this data is meant to stay constant and should not be modified.
A practical example is storing geographic coordinates. The latitude and longitude of a city do not change, so we can safely store them in a tuple to indicate they are fixed data.
# Tuple - immutable
coordinates = (40.7128, -74.0060) # NYC latitude, longitude
coordinates[0] = 41.8781 # TypeError: 'tuple' object does not support item assignment
Important detail to understand: if a tuple contains mutable objects like lists inside it, those objects themselves can still be modified. The tuple stores references (addresses) to objects, not the objects themselves. The immutability of the tuple protects the references it holds—we cannot change which objects the tuple points to, add new references, or remove them. However, if an object that the tuple points to is mutable, we can modify that object directly. The tuple itself does not change—we still cannot add or remove elements from the tuple—but the mutable objects within it can be changed.
# Tuple with mutable object inside
config = ('production', ['server1', 'server2'])
config[1].append('server3') # This works - modifying the list inside
print(config) # ('production', ['server1', 'server2', 'server3'])
Key Takeaways
- Lists are mutable: we can add, remove, and modify elements after creation
- Tuples are immutable: once created, they cannot be changed (though mutable objects inside them can be)
- Use lists for collections that change, such as to-do lists or shopping lists
- Use tuples for fixed data that should not change, such as coordinates or configuration values
- Tuples offer a slight performance benefit because Python does not need to manage changes
- Tuples communicate intent: other developers understand that the data is meant to be constant
Top comments (0)