Skip to content

Python List vs Tuple

What are the differences between Python lists and tuples, including syntax, mutability, available operations, and when to choose each data structure.

Lists and Tuples store one or more objects or values in a specific order.

Lists and tuples are similar in most context, but there are some differences that are discussed below.

Main Syntax Differences between Lists and Tuples

  1. Lists use square brackets [] while Tuples use parentheses ().
  2. Lists have variable length while tuple have fixed length.
  3. Lists are mutable while tuples are immutable.
  4. List have more functionality than tuples.

Creating List vs Creating Tuple

my_list = [1,2,3,4]
my_tuple = (1,2,3,4)

print(list_num) # [1,2,3,4]
print(tup_num)  # (1,2,3,4)

Mutable List vs Immutable Tuples

Lists are mutable, i.e., they can be changed or modified after they are created.

Tuples are immutable, i.e., they can't be changed or modified after they are created.

my_list = [1,2,3,4]
my_tuple = (1,2,3,4)

my_list[2] = 5
print(my_list) # [1,2,5,4]

my_tuple[2] = 5
# TypeError: 'tuple' object does not support item assignment

Operations

Lists have more built-in functions than tuples.

You can get all the functions associated with lists and tuples using dir([object]).

List

my_list = [1,2,3,4]
print(dir(my_list))

Output

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

Tuple

my_tuple = (1,2,3,4)

Output

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']

List or Tuple: which to choose?

It might seem that lists can always replace tuples, but tuples are also extremely useful data structures.

  1. Using a tuple instead of a list can give the programmer and interpreter a hint that the data should not be changed.

  2. Tuples are commonly used as the equivalent of a dictionary without keys to store data. For Example, tuples inside a list:

    [('Swordfish', 'Dominic Sena', 2001), ('Snowden', ' Oliver Stone', 2016), ('Taxi Driver', 'Martin Scorsese', 1976)]
  3. Reading data is easier when tuples are stored within a list. For example,

    [(1,2), (3,4), (5,6), (7,8)]

    is easier to read than

    [[1,2], [3,4], [5,6], [7,8]]
Navigation