Member-only story
Python Shorts — Lists vs. Tuples
The difference between a Python list and a Python tuple is actually quite simple, especially when explained with a simple example or two.
Is It a List or a Tuple?
A list has square bracket characters around it… [a,b,3]
. A tuple has parenthesis around its content… (a,b,3)
. This is the most notable difference between the two.
What Can Be in a List or Tuple?
Most often a list or tuple will contain a consistent set of numbers or a set of strings, but basically anything can be there, and you can mix it up all you want. Here, for example, is a Python script that contains and demonstrates a valid list that contains a number, string, variable, another list, a tuple, a dictionary, and a function:
import matha, b, x = 2, 3, 3.14list_mixed = [17, "apple", x, [a, b, 3], (4, 5, b),
{"name": "John"}, math.sin]y = list_mixed[6](0.5) # same as math.sin(0.5)print(a, b, x, y)
# 2 3 3.14 0.479425538604203
Can You Change a List or a Tuple?
You can change a list, but not a tuple, and this is the one thing that really differentiates between the two! The following code allows the list to be…