Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Traversing a Tuple

00:00 In Python, the tuple data type is an iterable type. This means that it conforms to the different ways you can iterate over an object. In this lesson, you’ll be exploring the three main ways of using iteration to traverse the elements of a tuple: for loops using Python’s for in syntax, list comprehensions, and generator comprehensions.

00:21 Back to the REPL we go. To start exploring for loops, first, define a tuple of tuples.

00:35 The tuple monthly_incomes contains six tuples. Each of those tuples has two values: a month name as a string, and an integer value. The first tuple is “Jan” and 5,000, the second “Feb”, and 5,500, and so on.

00:51 If you want to sum up the incomes, a loop would be the perfect way to do it. First, define a new variable, total_income, and set it to zero.

01:00 Now you can write the for loop. for income in monthly_incomes: then enter an indented block–total_income += income [1].

01:12 income is a temporary variable that will hold the value of each element as the loop iterates over the tuple. In this case, it will be one of the tuples holding a month name and an integer. For each element in the tuple, we use in-place addition to increment total income by the second value in the tuple, which is the integer.

01:32 Now examine total_income. That looks right to me. If in doubt, I’ll leave it to you to verify the math yourself. You can make this code a little bit cleaner by applying tuple unpacking to the header of the for loop.

01:46 Instead of the single income variable, you can instead unpack each tuple into its constituent elements. The month name, which is a string and income, which is an integer.

01:57 First reset total_income to zero.

02:01 Now write the for loop: for month, income in monthly incomes: total income += income.

02:15 And the result is the same, but written like this, your code is even more readable.

02:23 Okay, but what about comprehensions? Let’s look at those now. You’ll work with a tuple, numbers, that holds five numbers as strings: “2”, “9”, “5”, “1”, and “6”.

02:34 Because they’re strings, you probably want to convert them to integers. So how can you quickly convert this tuple of strings into a tuple of ints? This is a great use case for a list comprehension.

02:46 tuple([int (number)` for number in numbers]).

03:00 In this case, the comprehension iterates over each element in numbers, assigns the element to the temporary variable number and converts that element to an integer.

03:10 The result is a list of integers, which is then passed to the tuple constructor, finally producing our goal of an all integer tuple. You can take this even further. Since you’re already using tuples for memory efficiency, by replacing the square brackets of the list comprehension with round brackets or even omitting the brackets if syntactically valid, you’ll produce a generator comprehension.

03:35 Generator comprehensions can be even more efficient because elements from the generator are passed to the tuple constructor one at a time instead of creating an intermediate list object, as we did before.

03:52 And all you had to do was remove a couple brackets. for loops and comprehensions aren’t just for tuples, but for all iterable types in Python.

04:01 But it’s not just iteration that tuples have in common with other types. So next up, we’ll look at even more sequence operations you can perform with tuples.

Become a Member to join the conversation.