Demystifying Python Tuples

Hi there! This is the first tutorial in the "Python data structures" course. Our goal with this module is to help you become

familiar with advanced data structures that can be used in your scientific programming projects.

Let’s start our tutorial by discussing tuples, which are very similar to the Python lists we’ve discussed in the “Absolute

Beginners" course. Here’s a quick ‘list’ tutorial recap.

A list includes a series of items grouped together, like this:

colors = ['red', 'green', 'blue']

We can print out the value of any list element:

print (colors[1])

green

And we can change the properties of any list element:

Unlike lists, tuples can’t ever change their values, and they are defined using regular parentheses, not square brackets:

colors = ('red', 'green', 'blue')

We can read the values of the items inside a tuple, but we can’t change them. If we try to do that, we will get a Traceback

(an error):

So, tuples are immutable. But why would we want to use them when lists are much more flexible? Well, tuples are perfect for applications where we want to make sure data doesn’t change. We can look up and use each item in a tuple, and we can count the number of items in the tuple. However, we can’t edit, add or remove items from a tuple, or change the order of the items in a tuple.

However, the key advantage of tuples derives from their immutability: they are much faster and use computer memory much more efficiently than Python lists. So, they’re perfect for scientific programming projects that need to work with thousands, and sometimes even millions of data rows.

The easiest way to see all the methods available with tuples is to create a tuple, and then use the “dir” command:

As you can see, there aren’t too many methods available to us. We can’t sort tuples, we can’t append them, reverse them, etc. Still, they run like the wind :)

So, how can we solve a real-world scientific programming problem using tuples? Let’s assume that we have to compute and display the average grades for several students, and we want to preserve the individual grades earned by each one of them.

Believe it or not, this simple code snippet does exactly what we need:

students = [

    ("Dory", 88, 71, 76),

    ("John", 91, 78, 72),

    ("Jack", 78, 95, 60),

    ("Emma", 92, 74, 81)

]

def calculate_average(student_tuple):

    name = student_tuple[0]

    grades = student_tuple[1:]

    average = sum(grades) / len(grades)

    return (name, average)

print("Average Student Grades")

print("--------------------------")

for student in students:

    name, average = calculate_average(student)

print(f"{name}'s average grade: {average:.1f}")

First, we store the students’ names and their grades. Then we create a function that computes the average grade values and returns tuples that contain the students’ names and the average grade values. Finally, we display the students' names and their average grades.

Here’s what happens when we run the program:

This concludes our tuples tutorial. Stay tuned for the next one, where we will cover another powerful Python data structure – dictionaries.