Tuples

fruits = "banana", "apple", "orange"

or

fruits = ("banana", "apple", "orange")

Why immutables?

Why use tuples since they can’t change?

  • They uses less memory and demands less processing.
  • They don’t need to be copied when attributing to a new variable (see later).

What if I really want to change?

fruits[1] = "pineapple" # try changing "apple" to "pineapple"
TypeError: 'tuple' object does not support item assignment
  • To “change” a tuple, for example, you would basically need to either:

    1. Create a new tuple (with the changes you want), or
    2. Convert the tuple to a list, change the value you want (lists are mutable), and convert it back to a tuple.
      • Which would mean creating a new list object (from the tuple), and then creating a new tuple object (from the list)… which can be actually very inefficient and costly…