Python Programming Lesson #56
November 1, 2000
Viewing tip
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. 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
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:
# File Tuple06.py #------------------------------- # Create a simple tuple t1 = 1,2,3 # Create nested tuple t3 = t1,"B" t4 = "X",t3,"Y","Z" print "A nested tuple" print t4 Listing 1 |
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 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 ('X', ((1, 2, 3), 'B'), 'Y', 'Z') Listing 2 |
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" print t4[1:3] Listing 3 |
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.
A slice (((1, 2, 3), 'B'), 'Y') Listing 4 |
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
print "An indexed slice" print t4[1][0:1] Listing 5 |
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.
0 | 'X' |
1 | ((1, 2, 3), 'B') |
2 | 'Y' |
3 | 'Z' |
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 ((1, 2, 3),) Listing 6 |
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 # Rev 8/4/00 # Copyright 2000, R. G. Baldwin # Illustrates slicing nested # tuples - combines indexing # and slicing. #------------------------------- # Create a simple tuple t1 = 1,2,3 # Create/print nested tuple t3 = t1,"B" t4 = "X",t3,"Y","Z" print "A nested tuple" print t4 print "A slice" print t4[1:3] print "An indexed slice" print t4[1][0:1] print "Double-indexed slice" print t4[1][0][1:3] Listing 9 |
Double-indexed slice (2, 3) Listing 8 |
Ans: See Listing 7.
print "Double-indexed slice" print t4[1][0][1:3] Listing 7 |
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.
-end-