What is the difference between a Python list and a tuple?
Short answer
A list is MUTABLE (elements can be added, removed, or changed after creation), created with square brackets; a tuple is IMMUTABLE (fixed once created), created with parentheses — tuples are also slightly faster and can be used as dictionary keys or set elements, unlike lists.
Advertisement
AModel answer
A list is MUTABLE (elements can be added, removed, or changed after creation), created with square brackets; a tuple is IMMUTABLE (fixed once created), created with parentheses — tuples are also slightly faster and can be used as dictionary keys or set elements, unlike lists.
my_list = [1, 2, 3]
my_list.append(4) # OK: lists are mutable
my_tuple = (1, 2, 3)
# my_tuple.append(4) # Error: tuples have no append method, they're immutableReal-world example
Using a tuple for a fixed (x, y) coordinate pair that should never change, versus a list for a growing shopping cart.
Likely follow-ups
- Why can a tuple be used as a dictionary key, but a list cannot?
Related topics