What's the Difference Between Lists and Tuples in Python?
Quick Answer #
Lists are mutable (changeable) sequences, while tuples are immutable (unchangeable) sequences. This fundamental difference affects performance, use cases, and functionality.
Detailed Comparison #
1. Mutability #
Lists are mutable:
# You can modify lists after creation
my_list = [1, 2, 3]
my_list.append(4) # [1, 2, 3, 4]
my_list[0] = 10 # [10, 2, 3, 4]
my_list.remove(2) # [10, 3, 4]
Tuples are immutable:
# You cannot modify tuples after creation
my_tuple = (1, 2, 3)
# my_tuple.append(4) # AttributeError!
# my_tuple[0] = 10 # TypeError!
# my_tuple.remove(2) # AttributeError!
2. Syntax #
Lists use square brackets:
my_list = [1, 2, 3]
empty_list = []
single_item_list = [1]
Tuples use parentheses:
my_tuple = (1, 2, 3)
empty_tuple = ()
single_item_tuple = (1,) # Note the comma!
3. Performance #
Memory Usage:
import sys
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
print(sys.getsizeof(my_list)) # 104 bytes
print(sys.getsizeof(my_tuple)) # 80 bytes
Speed Comparison:
import timeit
# Creating lists vs tuples
list_time = timeit.timeit('[1, 2, 3, 4, 5]', number=1000000)
tuple_time = timeit.timeit('(1, 2, 3, 4, 5)', number=1000000)
print(f"List creation: {list_time:.4f}s")
print(f"Tuple creation: {tuple_time:.4f}s")
# Tuples are ~2x faster to create
When to Use Each #
Use Lists When: #
- You need to modify the data:
shopping_cart = [] shopping_cart.append("apples") shopping_cart.append("bread") shopping_cart.remove("bread")
- Data represents a collection of similar items:
temperatures = [20.1, 21.3, 19.8, 22.0, 20.9] scores = [85, 92, 78, 95, 88]
- You need list-specific methods:
numbers = [3, 1, 4, 1, 5] numbers.sort() # [1, 1, 3, 4, 5] numbers.reverse() # [5, 4, 3, 1, 1] numbers.extend([9, 2]) # [5, 4, 3, 1, 1, 9, 2]
Use Tuples When: #
- Data shouldn't change (coordinates, RGB values):
point = (10, 20) color = (255, 128, 0) date = (2024, 1, 15)
- Returning multiple values from functions:
def get_name_age(): return "Alice", 25 name, age = get_name_age() # Tuple unpacking
- Using as dictionary keys:
# Tuples can be dictionary keys (lists cannot) locations = { (0, 0): "origin", (1, 1): "northeast", (10, 20): "point A" }
- Configuration or settings:
DATABASE_CONFIG = ("localhost", 5432, "mydb", "user") RGB_RED = (255, 0, 0) SCREEN_SIZE = (1920, 1080)
Common Methods and Operations #
Both Support: #
# Indexing
my_list[0], my_tuple[0]
# Slicing
my_list[1:3], my_tuple[1:3]
# Length
len(my_list), len(my_tuple)
# Membership testing
1 in my_list, 1 in my_tuple
# Iteration
for item in my_list: pass
for item in my_tuple: pass
# Unpacking
a, b, c = my_list
x, y, z = my_tuple
List-Only Methods: #
my_list.append(item)
my_list.insert(index, item)
my_list.remove(item)
my_list.pop()
my_list.sort()
my_list.reverse()
my_list.extend(other_list)
my_list.clear()
Tuple-Only Benefits: #
# Can be used as dictionary keys
my_dict = {my_tuple: "value"}
# Slightly faster iteration
# Better memory efficiency
# Thread-safe (immutable)
Converting Between Lists and Tuples #
# List to tuple
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
# Tuple to list
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
# Creating from other iterables
my_tuple = tuple("hello") # ('h', 'e', 'l', 'l', 'o')
my_list = list("world") # ['w', 'o', 'r', 'l', 'd']
Common Gotchas #
1. Single Item Tuple #
# Wrong - this is not a tuple!
not_a_tuple = (1)
print(type(not_a_tuple)) # <class 'int'>
# Correct - need a comma
my_tuple = (1,)
print(type(my_tuple)) # <class 'tuple'>
2. Modifying Nested Mutable Objects #
# Tuples are immutable, but can contain mutable objects
my_tuple = ([1, 2], [3, 4])
my_tuple[0].append(3) # This works!
print(my_tuple) # ([1, 2, 3], [3, 4])
3. Performance Assumptions #
# Tuples are faster for creation and access
# But lists are more versatile
# Choose based on use case, not just performance
Summary #
Feature | List | Tuple |
---|---|---|
Mutability | Mutable | Immutable |
Syntax | [1, 2, 3] | (1, 2, 3) |
Performance | Slower | Faster |
Memory | More | Less |
Use Case | Dynamic data | Fixed data |
Dict Key | No | Yes |
Methods | Many | Few |
Rule of thumb: Use tuples for data that won't change, lists for data that will.