Lists vs. Tuples
in Python
Learn to create lists and tuples, understand mutability, and know when to use each data structure.
What Are Collections?
In Python, we often need to store multiple values together. Instead of creating separate variables for each item, we use collection types: containers that hold ordered sequences of values.
Creating a List
A list is defined with square brackets. It can hold any mix of data types: strings, integers, floats, even other lists.
# A list of strings fruits = ["apple", "banana", "cherry"] # A list of mixed types data = ["Alice", 17, 3.8, True] # An empty list empty = [] print(fruits[0]) # "apple" print(len(data)) # 4
Creating a Tuple
A tuple is defined with parentheses. Like lists, tuples store ordered items and allow indexing. The key difference: they are immutable.
# A tuple of coordinates point = (10, 20) # A tuple of colors colors = ("red", "green", "blue") # Single-element tuple needs a trailing comma solo = (42,) print(colors[1]) # "green" print(len(point)) # 2
The Core Rule: Mutability
Mutability means whether an object's contents can be changed after creation. This is the single most important distinction between lists and tuples.
Mutability in Action
fruits = ["apple", "banana", "cherry"] fruits[1] = "mango" # Replace item fruits.append("grape") # Add item fruits.remove("apple") # Remove item print(fruits)
['mango', 'cherry', 'grape']
colors = ("red", "green", "blue") colors[1] = "yellow" # Raises an error!
TypeError: 'tuple' object does not support item assignment
Lists vs. Tuples: Key Differences
| Feature | List | Tuple |
|---|---|---|
| Syntax | [ ] | ( ) |
| Mutable? | Yes | No |
| Add/Remove | append, remove, pop | Not allowed |
| Speed | Slightly slower | Slightly faster |
| Dict Key? | No | Yes |
| Use When | Data may change | Data stays fixed |
When to Use Each
How to Decide: Step by Step
Example 1: Shopping Cart
A shopping cart changes as users add and remove items. This calls for a list.
# Start with an empty cart cart = [] # Customer adds items cart.append("notebook") cart.append("pen") cart.append("eraser") print("Cart:", cart) # Customer removes an item cart.remove("pen") print("Updated:", cart) # Change an item cart[0] = "sketchbook" print("Final:", cart)
Cart: ['notebook', 'pen', 'eraser'] Updated: ['notebook', 'eraser'] Final: ['sketchbook', 'eraser']
Example 2: Fixed Settings
Screen resolution and RGB values should not change during a program. A tuple protects them from accidental modification.
# Screen resolution (should never change) resolution = (1920, 1080) # Access values by index width = resolution[0] height = resolution[1] print(f"Display: {width}x{height}") # Tuple unpacking (elegant shortcut) r, g, b = (255, 165, 0) print(f"Orange: R={r}, G={g}, B={b}")
Display: 1920x1080 Orange: R=255, G=165, B=0
Example 3: Converting Between Them
You can convert between lists and tuples using the list() and tuple() functions.
# Tuple to list (to modify, then back) colors = ("red", "green", "blue") # Convert to list to make changes color_list = list(colors) color_list.append("yellow") print("List:", color_list) # Convert back to tuple to lock it colors = tuple(color_list) print("Tuple:", colors)
List: ['red', 'green', 'blue', 'yellow']
Tuple: ('red', 'green', 'blue', 'yellow')
Useful List Methods
What They Share
Despite their differences, lists and tuples share many read-only operations. These work identically on both types.
data = [10, 20, 30, 40, 50] # or (10, 20, 30, 40, 50) # Indexing print(data[0]) # 10 print(data[-1]) # 50 (last item) # Slicing print(data[1:4]) # [20, 30, 40] # Membership test print(30 in data) # True # Iteration for item in data: print(item)
Common Mistakes
Summary: Lists vs. Tuples
Question 1 of 5
Which of the following correctly creates a tuple with a single element?
Question 2 of 5
What happens when you run my_tuple[0] = "new" on an existing tuple?
Question 3 of 5
You need to store student names that will be added and removed throughout a program. Which data structure is best?
Question 4 of 5
Which operation works on both lists and tuples?
Question 5 of 5
Why can tuples be used as dictionary keys but lists cannot?