Learn to Program using Python: Nested Tuples
Something for everyone
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 code fragments while you are reading about them.
Let's talk some more about tuples
The previous lesson (see Learn to Program using Python: Tuples, Index and Slice ) showed you
- How to create a tuple.
- How to access a tuple item using indexing.
- How to slice a tuple.
- Tuples are normally written as a sequence of items contained in matching parentheses.
- A tuple is an immutable sequence.
- Items in a tuple are accessed using a numeric index.
Tuples can contain other compound objects, including lists, dictionaries, and other tuples. Hence, tuples can be nested inside of other tuples.
Listing 1 shows the beginning of a Python script that
- Creates two tuples.
- Nests them in a third tuple.
- Determines the length (number of items) in the tuple containing the two nested tuples.
# File Tuple02.py #------------------------------- print "Create/print one tuple" t1 = 1,2 print t1 print "Create/print another \ tuple" t2 = "a","b" print t2 Listing 1
(The boldface in Listing 1 was added for emphasis.)
The remaining parts of this program are shown as code fragments in subsequent listings. A listing of the entire program is shown in Listing 7 near the end of the lesson.
Let's look at some output
Listing 2 shows the output produced by the code fragment in Listing 1.
Create/print one tuple (1, 2) Create/print another tuple ('a', 'b') Listing 2
Some of the lines in the output were highlighted in boldface for emphasis. The two lines highlighted in boldface in Listing 2 show the two tuples that were produced and printed by the code in Listing 1.
Now let's nest them
Listing 3 shows code that nests the two tuples, t1 and t2, produced earlier, along with two strings, in a new tuple. The new tuple is assigned to the variable named t3.
print "Create/print nested \ tuple" t3 = "A",t1,"B",t2 print t3 Listing 3
Nesting is easy
All that is required to nest the existing tuples in a new tuple is to list the variables representing the two existing tuples in a comma-separated list of items for creation of the new tuple.
What does the new tuple look like?
Listing 4 shows the printed output for the new tuple containing two nested tuples.
Create/print nested tuple ('A', (1, 2), 'B', ('a', 'b')) Listing 4
Nested tuples retain their identity
Note that the two nested tuples retain their identity as tuples, as indicated by the fact that the parentheses surrounding the items in the two nested tuples are preserved in the new tuple.
Let's get the length of the new tuple
Listing 5 shows code that gets and displays the length of the new tuple, which contains the two nested tuples.
print "Length of nested \ tuple is:" print len(t3) Listing 5
What is the length?
The length is a measure of the number of items in the tuple, and is obtained using the method named len().
The length is only four items
Listing 6 shows the output produced by the code in Listing 5, including the length of the new tuple containing the two nested tuples.
Length of nested tuple is: 4 Listing 6
The important point...
The important point is that even though the tuple shown in Listing 4 actually consists of six individual items (ignoring parentheses), each of the nested tuples is treated as a single item, giving a length of only four items for the tuple that contains the two nested tuples.
This would be true regardless of the length of the nested tuples.
Accessing an item in a nested tuple
Later, we will see that a double square-bracket indexing notation can be used to gain access to the individual items in tuples that are nested inside of other tuples.
# File Tuple02.py # Rev 7/31/00 # Copyright 2000, R. G. Baldwin # Illustrates creating and # displaying nested tuples # #------------------------------- print "Create/print one tuple" t1 = 1,2 print t1 print "Create/print another \ tuple" t2 = "a","b" print t2 print "Create/print nested \ tuple" t3 = "A",t1,"B",t2 print t3 print "Length of nested \ tuple is:" print len(t3) Listing 7
2. Write a Python script that illustrates how to nest two or more tuples inside another tuple.
Ans: See Listing 8.
# File Tuple11.py # Rev 7/31/00 # Copyright 2000, R. G. Baldwin # Illustrates tuple nesting # #------------------------------- t1 = "a",("b","c"),"d",("e","f") print t1 print len(t1) Listing 8
3. Write a Python script that illustrates how to determine the length of a tuple.
Ans: See Listing 8.
4. True or false? All that is required to nest existing tuples into a new tuple is to list the variables representing the existing tuples in a comma-separated list of items for creation of the new tuple.
5. True or false? When you nest one or more tuples inside another tuple, the items in the nested tuples are melted into the new tuple. That is to say, the length of the new tuple is the sum of the lengths of the nested tuples plus the non-tuple items in the new tuple.
Ans: False. Nested tuples retain their identity as tuples, and each nested tuple counts as only one item in the new tuple, regardless of the lengths of the nested tuples.
6. How do you access the individual items inside a tuple that is nested inside another tuple?
Ans: There is a double square-bracket indexing notation that can be used for this purpose.
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 authorRichard 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.