Preface
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.
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 code fragments while you are reading about them.
Introduction
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.
This lesson will expand your knowledge of tuples by teaching you about nesting tuples within other tuples.
What Is a Tuple?
Learn to Program using Python: Tuples, Index and Slice
, a tuple is like a list whose values cannot be modified. In other words, a tuple is immutable.
- 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 be nested
Tuples can contain other compound objects, including lists, dictionaries, and other tuples. Hence, tuples can be nested inside of other tuples.
Sample Program
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 |
(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 |
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 |
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 |
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 |
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: |
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.
Listing of Sample Program
# File Tuple02.py |
What’s Next?
Review
Ans: True.
2. Write a Python script that illustrates how to nest two or more tuples inside another tuple.
Ans: See Listing 8.
# File Tuple11.py |
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.
Ans: True.
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 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.