- What Is a Tuple?
- Sample Program
- What’s Next?
- Listing of Sample Program
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different figures and listings, without losing your place, while you are reading about them.
Something for everyone
Beginners start at the beginning, and experienced programmers jump in further along.
Learn to Program using Python: Lesson 1, Getting Started
provides an overall description of this online programming course. (You will find a consolidated index to all of my Python, Java, and XML tutorials on my website.)
Previous lessons have illustrated
- How to create (pack) a tuple.
- How to access a tuple item using indexing.
- How to slice a tuple.
- How to nest tuples.
- How to create empty tuples.
- How to create single-item tuples.
- How to unpack a tuple
- How to use a numeric index to access the items in nested tuples.
This lesson will teach you how to combine indexing and slicing to access groups of items in nested tuples.
Looks a lot like list processing to me…
By the way, in case you haven’t figured it out before now, almost everything that I have been teaching you about tuples can also be applied to lists.
Listing 1 shows the beginning of a Python script that:
- Packs a multiply-nested tuple by successively nesting tuples in new tuples.
- Gets and displays a slice of the top-level tuple.
- Uses a combination of indexing and slicing to access “sub-tuples” that are nested at different levels in the top-level tuple.
- Slices and displays groups of items in the sub-tuples.
# File Tuple06.py
The remaining parts of this program are shown as code fragments in subsequent figures. A listing of the entire program is shown in Listing 9.
Tuples support slicing
As shown in Listing 1, tuples support the [x:y] slicing operation.
You may recall that the slicing operation returns a slice from the sequence beginning at index value x and including all items up to, but not including, the item at index value y.
There are some special cases, and the general slicing rules were discussed in detail in an earlier lesson entitled
Learn to Program using Python: Strings, Part II
Pack the multiply-nested tuple
The code in Listing 1 packs the nested tuple by successively nesting an existing tuple into a new tuple. This results in a tuple that is nested several levels deep as shown by the program output in Listing 2.
A nested tuple
What is the length of the tuple?
Although this program doesn’t include the use of the len() method, it is easy enough to determine by inspection that the tuple has a length of four items. Those four items are listed on separate lines in the chart in Figure 1 to help you identify them. In addition to listing the items on separate lines, the chart also provides the index value for each item.
Figure 1 Chart Showing Four Items
What are the available index values?
The four items contained in the tuple can be accessed using index values of 0, 1, 2, and 3.
Everything that is highlighted in red in Listing 2 is a single item, which can be accessed using the index value of 1. This item appears in the second line of the above chart.
This item is a nested tuple, which also contains another nested tuple. I will be making use of this fact later when I combine indexing with slicing. But for now…
Let’s concentrate on slicing alone
Listing 3 shows a simple slice applied to the top-level nested tuple shown in Listing 2. This slicing syntax means to get and return the set of items in the tuple named t4 beginning with the item at index value 1 and ending at index value (3-1) or 2.
print “A slice”
The item at index 1 is a tuple
Listing 4 shows the output produced by the code in Listing 3. If you compare this output with the chart given earlier, you will see that the group of items from index value 1 to and including index value 2 were extracted and used to produce a new tuple.
I highlighted the item extracted from index value 1 in red and the item extracted from index value 2 in blue to make it easier for you to identify them.
The item extracted from index value 1 is itself a tuple that contains another nested tuple.
The next thing that I am going to do is to extract this tuple using an index and apply a slicing operation to it.
Indexing plus slicing
Listing 5 shows a code fragment that combines indexing and slicing. This fragment
- Extracts the item at index value 1 from the top-level tuple named t4. This item is itself a tuple.
- Applies a slicing operation that extracts the group of items from index value 0 up to, but not including, index value 1.
print “An indexed slice”
Doesn’t that mean a group of one item?
Stated differently, the slicing operation in this case selects a group of items where the number of items in the group is one beginning at the item whose index value is 1.
What does the group of one look like?
Now consider once more the different items in the top-level tuple named t4. I have reproduced the earlier chart below for viewing convenience.
|1||((1, 2, 3), ‘B’)|
Figure 2 Items in the Top-Level Tuple
Let’s be different
Just to be a little different, in this case, I highlighted the item at index value 1 by using washed-out colors for the other items. I also used color to identify the two items contained in the sub-tuple item at index value 1.
The item at index value 0 in the sub-tuple is highlighted in red. The item at index value 1 in the sub-tuple is highlighted in blue.
Let’s see some output
Listing 6 shows the output produced by the code in Listing 5. As you can see, the output in this case is a single-item tuple, and that item is the item highlighted in red in the above chart.
An indexed slice
I’m going to leave the final step in this program as an exercise for the student. See the question in the Review section.
# File Tuple06.py
Ans: See Listing 7.
print “Double-indexed slice”
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.