Tuples

Tuples

Lists are mutable:

my_list = [1, 2, 3]
my_list[1] = "NEW VALUE" # This has changed the list forever

Tuples are immutable:

  • Accessing the elements within a tuple is the same as for a list

  • You can use slicing the same way as with lists

my_tuple = (1, 2, 3, "some data", [1, 2, 3])
print(my_tuple[-1]) # Prints the list from the tuple

my_tuple[3] = "other data" # Tuples don't support item assignment

You can change the items of a list within a tuple:

my_tuple[-1][0] = "other data"
print(my_tuple) 
# Will print (1, 2, 3, "some data", ['other data', 2, 3])

Last updated