Tuples

Learn how tuples work in Python.

This lesson explains the concepts of tuples, which are mostly overlooked when programming in Python.

Introduction

A tuple is another container sequence offered by the standard Python 3 library. It is an immutable sequence.

Explanation

Keeping the records

A tuple isn’t just an immutable list. It also serves another ulterior motive, i.e., we can also use tuples for the record-keeping purpose. It’s simple but mostly overlooked. So we’ll cover it in this section.

Presuming you know the basics of a tuple and its syntax, look at the following tuples:

day = ('Monday', 1)
name, color, weight = ('banana', 'yellow', 10)

The above tuples hold data in them as follows:

  • day: Holds data about Monday, where the value 1 means it’s the first day of the week.
  • name, color, weight: Holds data about a fruit e.g., a banana.

Unpacking the tuples

Retrieving the items separately from a tuple is known as unpacking a tuple. The above parallel assignment name, color, weight = ('banana', 'yellow', 10) is the most simple example of unpacking a tuple. In case we have a list of tuples, a for loop serves the purpose of unpacking them.

Run the following program to see how to unpack a tuple with a for loop.

Press + to interact
fruits = [('banana', 'yellow', 10), ('apple', 'red', 19)]
# Unpacking the tuple with a for loop
for name, _, _ in fruits:
print(name)

In the code above, you can see (at line 4) that we overlooked the last two items of every tuple present in fruits, using a dummy variable _. That is because we only needed to print the name at line 5. In case you want to retrieve all the fields, try replacing _ with the variables, and print them in the next line.

You may be surprised to know that you can unpack a tuple without even using an assignment statement or a for loop. You can unpack a tuple using a * operator. This functionality is mostly used when passing a tuple’s values as arguments to a function.

Run the following program.

Press + to interact
# Function returning the sum of its arguments
def add(a, b):
return a+b
numbers = (1, 2)
result = add (*numbers) # Unpacking numbers with * : add(1,2)
print(result)

In the code above (at line 2), you can see a function add that takes two parameters a and b and returns their sum. Now, we have a tuple numbers at line 5. Instead of utilizing a few lines of code to get the values from numbers, we simply pass *numbers as a parameter when calling the add function at line 6.

This * operator makes it possible to unpack a tuple and call a function within the same line, making the program more readable and efficient.

Be careful when unpacking a nested tuple with *. For example, if you have the following nested tuple:

nested = (1, 2, (4, 5))

Unpacking with * will give three values: 1, 2, and (4, 5). An important thing to note is that the inner tuple doesn’t break. You’ll need to unpack it one more time to get 4 and 5 separately.

Note: Python 3 doesn’t assist in sending a nested tuple as a parameter to the function, unlike the versions before Python 3. So doing anything like: def my_function(a, b, (c, d)) will give an error. For more details, follow this link.