01 / 20

Lists vs. Tuples
in Python

Learn to create lists and tuples, understand mutability, and know when to use each data structure.

Python

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.

List
Created with square brackets [ ]. Items can be changed after creation.
Tuple
Created with parentheses ( ). Items cannot be changed after creation.

Creating a List

A list is defined with square brackets. It can hold any mix of data types: strings, integers, floats, even other lists.

Python
# 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.

Python
# 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.

List (Mutable) "apple" "banana" "mango" → "cherry" ✓ Can change items Tuple (Immutable) "apple" "banana" "cherry" ✕ Cannot change items

Mutability in Action

Modifying a List (works)
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"       # Replace item
fruits.append("grape")    # Add item
fruits.remove("apple")    # Remove item
print(fruits)
['mango', 'cherry', 'grape']
Modifying a Tuple (fails)
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

Use a List
Shopping cart items, student grades, to-do tasks: anything that grows, shrinks, or changes over time.
Use a Tuple
GPS coordinates, RGB colors, days of the week: data that is fixed and should not be modified.
List Examples cart = ["shirt", "hat"] scores = [88, 92, 75] tasks = ["read", "code"] Tuple Examples origin = (0, 0) rgb = (255, 128, 0) days = ("Mon", "Tue"...)

How to Decide: Step by Step

1
Identify the data. What values will you store? How many?
2
Ask: will it change? If you need to add, remove, or replace items later, choose a list.
3
Ask: should it be protected? If the data must stay fixed (coordinates, config settings), choose a tuple.
4
Write it out. Lists use [ ], tuples use ( ). Remember the trailing comma for single-element tuples.
5
Test your assumption. Try modifying the collection. If a tuple raises a TypeError, that is by design.

Example 1: Shopping Cart

A shopping cart changes as users add and remove items. This calls for a list.

Python
# 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.

Python
# 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.

Python
# 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

.append(x)
Add item x to the end of the list.
.insert(i, x)
Insert item x at position i.
.remove(x)
Remove the first occurrence of x.
.pop(i)
Remove and return item at index i. Default: last item.
.sort()
Sort the list in place (modifies the original).
.reverse()
Reverse the list in place.

What They Share

Despite their differences, lists and tuples share many read-only operations. These work identically on both types.

Works on Both Lists and Tuples
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

✕ Forgetting the Trailing Comma
Writing (42) creates an integer, not a tuple. Use (42,) for a single-element tuple.
✕ Trying to Modify a Tuple
Calling .append() or assigning to an index on a tuple raises a TypeError. Convert to a list first.
✕ Using a List as a Dict Key
Lists are mutable and unhashable, so they cannot be dictionary keys. Use a tuple instead.

Summary: Lists vs. Tuples

Python Sequences List [ ] ✓ Mutable append, remove, pop sort, reverse, insert ✕ Not hashable Best for dynamic data Slightly more memory Tuple ( ) ✕ Immutable count, index only Supports unpacking ✓ Hashable (dict key) Best for fixed data Slightly less memory

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?