1. Meet the tuples
The next container type we want to learn about is the tuple. Tuples are widely used internally by many of the systems we depend on like databases.
2. Tuple, tuple
Tuples are very much like lists they hold data in the order, and we can access elements inside a tuple with an index. However, the similarities end there. Tuples are easier to process and more memory efficient than lists. Tuples are immutable, which means we can't add or remove elements from them. This is powerful because we can use them to ensure that our data is not altered. We can create tuples by pairing up elements. Finally, we can use something called unpacking to expand a tuple into named variables that represent each element in the tuple. Let's explore these features.
3. Zipping tuples
Often, we'll have lists where we want to matchup elements into pairs, and the zip function enables us to do that. Here I've got a list for the most popular cookies in the US and India, and I want to build a list of pairs by the popularity rank of the cookie in each country and I'll pass them the zip function. Then I can print the result of my zip and you can see I have what looks like a list of Tuples. It's really an iterator, but that's for a different course. Notice that the tuples use a parenthesis as their object representation.
4. Unpacking tuples
Now let's look at how we can use unpacking with a tuple. Tuple unpacking, also sometimes called tuple expansion, allows us to assign the elements of a tuple to a named variable for later use. This syntax allows us to create more readable and less error prone code. Here I have a tuple containing the top ranked cookie from both countries, and I want to store them as us_num_1 and in_num_1. So that I can print them by name. I start by putting both variables as the target of the assignment statement separated by a comma. Then assign the first tuple in our top_pairs list to them. Let's look at a another great use case for tuple unpacking.
5. More unpacking in Loops
For me the place I use tuple unpacking the most is when working with loops. We can use tuple unpacking when declaring the for loop, to separate a list of tuples into their elements as we loop over them. This sounds a bit strange, but let's take a look at it. Here I'm building a for loop that uses tuple unpacking when iterating over the top_pairs list. It splits each tuple in the list into its Indian and US cookie elements. We then use each of these variables to print the cookies in order. Another use of tuple unpacking helps us keep track of which element in the iterable or list we are currently on.
6. Enumerating positions
Often we want to know what the index is of an element in the iterable is. The enumerate function enabled us to do that by creating tuples where the first element of the tuple is the index of the element in the original list, then the element itself. We can use this to track rankings in our data or skip elements we are not interested in. Here I'm going to enumerate our top pairs list and split that resulting tuple into idx and item. I can also use tuple unpacking on the item to get all three components separately. This can be exceptionally powerful. Let's look at a bit of responsibility that comes with this power.
7. Be careful when making tuples
When we are creating tuples, we can make them with zip or enumerate or use parentheses as shown here. However the real magic for creating a tuple in Python is the comma, if we accidentally end a line with a comma, we can create a tuple! This can have some very undesirable side affects further down in our code. Keep this in mind if you get a tuple where you don't expect it.
8. Let's practice!
Now that we've met the tuples, we're ready to explore how we can use them.