Python Basics — Tuples vs Lists

2 minute read

Tuples vs Lists

This is one of the most common Python interview questions. Both are ordered sequences, but they differ in fundamental ways.

Comprehensive Comparison

Feature List Tuple
Syntax [1, 2, 3] (1, 2, 3)
Mutable Yes — can add/remove/change No — fixed after creation
Methods 11 built-in methods 2 methods (count, index)
Hashable No Yes (if all elements hashable)
Dict key Cannot be used Can be used
Set element Cannot be used Can be used
Memory More (extra overhead for mutation) Less (optimized)
Speed Slightly slower Slightly faster
Use case Dynamic collections Fixed records

Mutability Comparison

# List — mutable
fruits = ["apple", "banana"]
fruits.append("cherry")
fruits[0] = "avocado"
print(fruits)  # ['avocado', 'banana', 'cherry']

# Tuple — immutable
colors = ("red", "green")
# colors.append("blue")  # AttributeError
# colors[0] = "yellow"   # TypeError

Performance Difference

Memory

import sys

lst = [1, 2, 3, 4, 5]
tpl = (1, 2, 3, 4, 5)

print(sys.getsizeof(lst))  # 104 bytes (varies by system)
print(sys.getsizeof(tpl))  # 80 bytes (typically ~20% less)

Speed — Creation

import timeit

# List creation
list_time = timeit.timeit("[1, 2, 3, 4, 5]", number=1_000_000)

# Tuple creation
tuple_time = timeit.timeit("(1, 2, 3, 4, 5)", number=1_000_000)

print(f"List:  {list_time:.4f}s")
print(f"Tuple: {tuple_time:.4f}s")
# Tuples are typically 5-10x faster to create

Speed — Iteration

import timeit

lst = list(range(1000))
tpl = tuple(range(1000))

list_time = timeit.timeit(lambda: sum(lst), number=10_000)
tuple_time = timeit.timeit(lambda: sum(tpl), number=10_000)

print(f"List:  {list_time:.4f}s")
print(f"Tuple: {tuple_time:.4f}s")
# Similar speeds for iteration (marginal difference)

Use Case Guidelines

When to Use Lists

# Dynamic collection — items will change
shopping_cart = []
shopping_cart.append("milk")
shopping_cart.append("bread")
shopping_cart.remove("milk")

# Homogeneous data (all same type)
temperatures = [72.1, 73.5, 71.8, 74.2]

# Need sorting/filtering
scores = [85, 92, 78, 95, 88]
scores.sort()

When to Use Tuples

# Fixed records
coordinates = (40.7128, -74.0060)  # latitude, longitude
rgb_color = (255, 128, 0)

# Return multiple values from functions
def divide(a, b):
    return a // b, a % b

quotient, remainder = divide(17, 5)

# Dictionary keys
locations = {
    (40.7128, -74.0060): "New York",
    (51.5074, -0.1278): "London",
}

# Constants that shouldn't change
DIRECTIONS = ("north", "south", "east", "west")
HTTP_METHODS = ("GET", "POST", "PUT", "DELETE")

Conversion Between Types

# Tuple to list
colors_tuple = ("red", "green", "blue")
colors_list = list(colors_tuple)
colors_list.append("yellow")
print(colors_list)  # ['red', 'green', 'blue', 'yellow']

# List to tuple
numbers_list = [1, 2, 3, 4, 5]
numbers_tuple = tuple(numbers_list)
print(numbers_tuple)  # (1, 2, 3, 4, 5)

Quick Decision Rule

Think of it this way:

  • List → “I have a collection of similar items that might grow or shrink” (shopping cart, TODO items)
  • Tuple → “I have a fixed record where position has meaning” (coordinates, RGB color, database row)
# This is naturally a tuple — (name, age, email) is a record
person = ("Alice", 30, "alice@example.com")

# This is naturally a list — variable number of similar items
students = ["Alice", "Bob", "Charlie"]

Summary

  • Lists are mutable, tuples are immutable
  • Tuples use less memory and are faster to create
  • Use tuples for fixed records, lists for dynamic collections
  • Tuples can be dict keys and set elements — lists cannot
  • Convert with list() and tuple()
  • When in doubt, start with a tuple — convert to list only if mutation is needed

Next up: Tuples — Practice Exercises →

Updated: