Tuples
An Expected Failure
This function is supposed to return both the smallest and largest value in a given list:
def minmax(items):
return min(items)
return max(items)
There’s a bug in our code though!
Currently the function only returns the smallest item:
>>> minmax([8, 2, 7, 5, 9, 3, 4])
2
Try to figure out a way to return both the smallest and largest items from this function.
Tuple Basics
Tuples are basically lists that you aren’t allowed change.
We say that lists are “mutable” (you can “mutate” them) and tuples are “immutable” (they cannot be “mutated”).
So tuples are ordered groups of items, just like lists, but they cannot be modified. We use parentheses to indicate we are making a tuple and not a list (or other object).
>>> time = (10, 30)
>>> time
(10, 30)
We can see that if we try to change our tuple, an exception is raised because, just like strings, tuples are immutable:
>>> time[0] = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Parentheses are usually optional when making a tuple:
>>> coordinates = 3, 4, 5
>>> coordinates
(3, 4, 5)
How can we make a tuple with a single item, just the number 3, in it?
>>> numbers = (3)
>>> numbers
3
That didn’t work.
Putting parenthesis around something doesn’t make it a tuple. It’s the commas that make a tuple.
Adding a trailing comma in the parentheses will make a tuple containing a single item:
>>> numbers = (3,)
>>> numbers
(3,)
Again we can leave off the parentheses here if we want because there is no ambiguity:
>>> numbers = 3,
>>> numbers
(3,)
>>> type(numbers)
<class 'tuple'>
You can make an empty tuple with empty parentheses:
>>> empty = ()
>>> empty
()
>>> type(empty)
<class 'tuple'>
When we are making a function, what if we need multiple return values? The idiomatic way to do this in Python is to use a tuple to return multiple values. We probably won’t need this often, but it’s nice to know it’s possible, and you will see this now and then in the real world.
Let’s make a function that returns the number of cakes and wine bottles that we need for a party, given a specified number of guests.
>>> def get_cake_and_wine(guests):
... cakes = guests / 8
... wine_bottles = guests / 3
... return cakes, wine_bottles
...
>>> get_cake_and_wine(10)
(1.25, 3.3333333333333335)
Note the return statement has multiple items. Python puts the two items together into a tuple and returns the tuple.
So what are tuples good for?
useful for returning multiple values from a function
sometimes used as immutable collections (often of known length)
heterogenous collections of things (taking a couple things and storing them in a variable together)
List & Tuple Arithmetic
We already learned how the addition and multiplication operators work on strings. These same operators also work on lists and tuples.
Addition on tuples:
>>> numbers = (5, 8)
>>> more_numbers = numbers + (7, 9)
>>> more_numbers
(5, 8, 7, 9)
Addition on lists:
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
Multiplication with tuples:
>>> (0, 1) * 2
(0, 1, 0, 1)
We can use multiplication on lists to initialize lots of elements with the same value.
For example we can make a list with 10 zeroes in it like this:
>>> my_list = [0] * 10
>>> my_list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
List & Tuple Comparisons
Tuples and lists both use deep equality. Two tuples are equal when they have the same number of items and the item at each index in the tuples is equal.
>>> p = (1, 1, 1)
>>> q = (1, 1, 1)
>>> p == q
True
Tuples can also be compared. Tuple comparisons work by comparing each element index-by-index, starting with the first ones:
>>> p = (1, 3, 9)
>>> q = (1, 2, 4)
>>> p < q
False
>>> p > q
True
This is called lexicographical ordering, which essentially means “alphabetical-like ordering”.
You can read more about tuple ordering here.