Loading video player…

Retrieving Elements From a Tuple

00:00 Once a tuple exists, how can you retrieve elements from it? You can access values in a tuple using square brackets. Because tuple are ordered and indexable, single elements can be retrieved by the index starting at position zero.

00:14 So the element at index zero will be the first element in the tuple. Because tuples are also sliceable, you can retrieve multiple elements by using slicing, which returns a new tuple.

00:26 Now let’s go back to the REPL to see these concepts in action. Back in the REPL now, let’s bring back Jane whose data could potentially be a row from a database.

00:37 Jane is a tuple with four values. The first being the string “Jane Doe”, the second the integer 25, the third, a float 1.75, and the fourth, a string “Canada”.

00:49 So if you want to access an individual element, you can select it by index starting at zero. The first element “Jane Doe” can be found at jane[0], the second, jane[1],

01:02 and the fourth, jane[3].

01:06 Now what if you try to access an element that doesn’t exist, such as an index larger than the number of elements in the tuple?

01:14 That earns you a one-way ticket to error city. Specifically, this causes an IndexError because the tuple index is out of range. You can also use negative indexing, however, these count backwards starting at negative one, which represents the last element in the tuple. jane[-1] returns “Canada”, the string, that is the last element in the tuple. jane[-2] returns the third element in the tuple because that is the second to last element, and so on it goes.

01:47 But of course, you’re still prone to the same errors if you try to access an element by index that isn’t there.

01:53 And yep, another IndexError. Once again, the tuple index is out of range because there are only four elements in the tuple.

02:04 Because tuples can hold other tuples, you may need to access nested values. Indexing works fine for this as well. First, you need a nested tuple.

02:15 This is John, a Python developer who’s definitely been studying with Real Python. The outer tuple contains four elements, a string, “John”, an int, 35, another string “Python Developer”, and a tuple, which itself contains four values: “Django”, “FastAPI”, “HTML”, and “CSS”—all strings.

02:37 Looks like John works in web. To access a nested value, you can chain accessors one after the other: employee[3][1]. This retrieves the fourth element, so the element at index three of the outer tuple, that itself is a tuple.

02:56 And then from that tuple, you retrieve the second element, which is index one of that tuple. And so we get the string “FastAPI”.

03:06 Now you can try slicing. Again you need a tuple to work with.

03:11 days is a tuple containing the days of the week, all as strings, starting with “Monday” and going through to “Sunday”. Just like accessing individual elements, the syntax for slicing uses square brackets.

03:24 However, between the square brackets, you can use up to three numbers separated by colons. The first number is start, the index position where the slice begins.

03:34 The second is stop the index where the slice ends, not inclusive of that value. The third is step the frequency of values to select starting from the first value.

03:46 So a step size of one would select every value, a step size of two, selects every second value, etc, etc. To select the first five elements, you can use days [:5].

03:59 The result is a tuple with the first five elements of the original tuple. If you’d like to select a subset with a specific start and endpoint, you can do that as well by providing two values in your slice.

04:12 For example, days[1:4] will return the second through fourth elements of the original tuple as a tuple. So in this case, we get “Tuesday”, “Wednesday”, and “Thursday”.

04:27 And of course, if you’d like to select every other day, for example, you can use a step size of two.

04:35 By leaving the start and stop arguments blank you default to selecting every element, and by having a step size of two, you select every second element starting from the first.

04:46 The result is “Monday”, “Wednesday”, “Friday”, and “Sunday”.

04:51 Lastly, you can use this syntax to conveniently reverse a tuple: days[::-1]. I know it looks weird, but it works. With this, you’re setting the start and stop arguments to their defaults and setting the step size to negative one. Defaults start and stop indicate you want the slice to retain the whole tuple, and the negative one step size means you’d like the slice to walk backwards one step at a time through the tuple. And like so, the output of the slice is all of the same elements, but in reverse order.

05:25 Remember, each slice produces a new tuple object. These are not in place mutations. And speaking of mutations, the next lesson is all about tuple immutability.

Become a Member to join the conversation.