Python List vs Tuple
Lists and Tuples store one or more objects or values in a specific order.
Objects stored in a list or tuple can be of any type, including the "null" type defined by the None
keyword.
Lists and tuples are similar in most context, but there are some differences that are discussed below.
Main Syntax Differences between Lists and Tuples
- Lists use square brackets
[]
while Tuples use parentheses()
. - Lists have variable length while tuple have fixed length.
- Lists are mutable while tuples are immutable.
- 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)
The type()
function gives the type of an object.
print(type(my_list)) # list
print(type(my_tuple)) # tuple
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
Since tuples are immutable, their size is fixed, while lists are of variable size.
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.
-
Using a tuple instead of a list can give the programmer and interpreter a hint that the data should not be changed.
-
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)]
-
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]]