= "banana", "apple", "orange" fruits
Tuples
- As every sequence type, it is ordered and subscriptable with integer indexes.
- Immutable, represented just as values separated with commas, or separated with commas and enclosed with parentheses:
or
= ("banana", "apple", "orange") fruits
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?
1] = "pineapple" # try changing "apple" to "pineapple" fruits[
TypeError: 'tuple' object does not support item assignment
To “change” a tuple, for example, you would basically need to either:
- Create a new tuple (with the changes you want), or
- 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…