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.
Introduction
Plus some other structures
It also introduced you to subscriptions, sequences, mutable sequences, mappings, slicings, and tuples.
Manipulating lists
That lesson showed you some of the ways that you can manipulate lists. The discussion was illustrated using sample programs.
Other ways to manipulate lists
This lesson carries that discussion forward by using sample programs to teach you other ways to manipulate lists.
Some Sample Programs
You can replace a slice in a list with the elements from another list through assignment.
Can change the length of the list
Note that this operation can change the length of the original list.
Replacing a slice in a list with the elements from another list is illustrated in Figure 1.
#
File Lists04.py
# Rev 2/4/00 # Copyright 2000, R. G. Baldwin # Illustrates replacing a slice # #------------------------------- print "Create and print a list" listA = [100,200,300,400,500] print listA print "Original length is:" print len(listA) print "Replace a slice" listA[1:4] = [2,4,8,16,32,64] print "Print the modified list" print listA print "Modified length is:" print len(listA) Figure 1 |
The function named len()
This program also illustrates the use of the function named len() to get and print the length of the list.
Replaces a three-element slice with a six-element list
In this program, a slice of an original five-element list, consisting of the elements from 1 through 3 inclusive, is replaced by the elements of a new list consisting of six new elements.
Length of the list is increased by 3
Since three existing elements are replaced by six new elements, the overall length of the list is increased by three elements.
Program output
The output from this program is shown in Figure 2 (boldface added for emphasis).
D:\Baldwin\AA-School\PyProg>python
Lists04.py
Create and print a list [100, 200, 300, 400, 500] Original length is: 5 Replace a slice Print the modified list [100, 2, 4, 8, 16, 32, 64, 500] Modified length is: 8 D:\Baldwin\AA-School\PyProg>
Figure 2 |
Six new elements replaced three original elements
As you can see, the six new elements replaced the three original elements to increase the length of the list from 5 to 8 elements.
Replacing an element with a list
It is also possible to replace an element in an existing list with a new list, as illustrated in the following program.
Behavior is different from above
In this case, the behavior is different from that shown above where a slice from the original list was replaced with the elements from a different list (even though the right operand of the assignment operator is the same in both cases).
Produces nested lists
When a single element is replaced by a list, the result is that a new list is nested inside the original list.
Length is unchanged
It is also interesting to note that the length of the list is unchanged by this operation since the list that replaces the element is itself considered to be a single element. Therefore, the number of elements is not changed.
Sample program
This is illustrated in Figure 3, where the element at index 2 of an original list is replaced with a new list having six elements.
#
File Lists05.py
# Rev 2/4/00 # Copyright 2000, R. G. Baldwin # Illustrates replacing an # element with a slice # #------------------------------- print "Create and print a list" listA = [100,200,300,400,500] print listA print "Original length is:" print len(listA) print "Replace an element" listA[2] = [2,4,8,16,32,64] print "Print the modified list" print listA print "Modified length is:" print len(listA) Figure 3 |
Program output
The output from this program is shown in Figure 4 (boldface added for emphasis).
D:\Baldwin\AA-School\PyProg>python
Lists05.py
Create and print a list [100, 200, 300, 400, 500] Original length is: 5 Replace an element Print the modified list [100, 200, [2, 4, 8, 16, 32, 64], 400, 500] Modified length is: 5 D:\Baldwin\AA-School\PyProg>
Figure 4 |
One element is itself a list
As you can see, the result is that one of the elements in the original five-element list is replaced by a new list containing six elements. However, the length of the list is unchanged.
Again, it is important to note that this results in one list being nested inside of another list.
Extracting elements from a nested list
Now I am going to illustrate the syntax for extracting elements from a nested list using pairs of matching square brackets. Figure 5 is an expansion of Figure 3.
print "Extract and display
each"
Figure 5 Note the boldface statements The most interesting statements in this program are highlighted in boldface. Display the nested list combination After nesting a list as element 2 in another list, the program displays the value of each of the elements of the original list. When element 2 is displayed, it can be seen to be another list. Double-square-bracket notation Then the program uses double-square-bracket notation (listA[2][4]) to extract and display each of the elements in the nested inner list that comprises element 2 of the outer list. Program output The output from this program is shown in Figure 6 (boldface added for emphasis).
D:\Baldwin\AA-School\PyProg>
Figure 6 More on nested elements The program in Figure 7 illustrates some additional aspects of nested elements.
Figure 7 Create a list of lists This program defines a three-element list containing three nested lists. In other words, each of the elements in the outer list is itself a list. Inner lists are different lengths Furthermore, the lengths of the inner nested lists are not the same. The lengths of the inner nested lists are 2, 3, and 4 elements each, respectively. Program behavior and output The output from the program is shown in Figure 8 (boldface added for emphasis). The program displays the entire list, and then gets and displays the lengths of each of the nested lists.
D:\Baldwin\AA-School\PyProg>
Figure 8 Getting the length of a nested list Note in particular the syntax used to pass a parameter to the len() method in order to get the length of a nested list ( len(listA[1]) ). Arrays -- Not for beginners If you are a beginning programmer, just skip this section. If you are an experienced programmer, you may have observed that a Python lists bear a striking resemblance to arrays in other programming environments. Python lists are more powerful However, Python lists are more powerful than the arrays I am aware of in other programming environments, including Java. Sub-arrays can be different sizes For example as in Java, when a Python list is used to construct a multidimensional array, the sub arrays don't have to be of the same size. Types can also be different However, unlike Java, the elements in the array don't even have to be of the same type (granted that the elements in a Java array can be of different types so long as there is an inheritance or Interface relationship among them). A three-dimensional array program In any event, the program in Figure 9 might represent what an experienced programmer would consider to be a three-dimensional array of integer data in some other programming environment.
Figure 9 Triple-square-bracket notation Pay particular attention to the triple square bracket notation that is used to access and print each element in the array. Program behavior This program The output from the program is shown in Figure 10.
D:\Baldwin\AA-School\PyProg>
Figure 10
#
File Lists06.py
# Rev 2/4/00
# Copyright 2000, R.
G. Baldwin
# Illustrates extracting
a
# list element
and extracting
# elements from
a nested list
#
#-------------------------------
print "Create and print
a list"
listA = [100,200,300,400,500]
print listA
print "Original length
is:"
print len(listA)
print "Replace an element"
listA[2] = [2,4,8,16,32,64]
print "Print the modified
list"
print listA
print "Modified length
is:"
print len(listA)
print "Extract and display
each"
print " element in the
list"
print listA[0]
print listA[1]
print listA[2]
print listA[3]
print listA[4]
print " element in nested
list"
print listA[2][0]
print listA[2][1]
print listA[2][2]
print listA[2][3]
print listA[2][4]
print listA[2][5]
D:\Baldwin\AA-School\PyProg>python
Lists06.py
Create and print a list
[100, 200, 300, 400, 500]
Original length is:
5
Replace an element
Print the modified list
[100, 200, [2, 4, 8, 16, 32,
64], 400, 500]
Modified length is:
5
Extract and display each
element in the list
100
200
[2, 4, 8, 16, 32, 64]
400
500
Extract and display each
element in nested
list
2
4
8
16
32
64
#
File Lists07.py
# Rev 2/4/00
# Copyright 2000, R.
G. Baldwin
# Illustrates more nested
# elements
#
#-------------------------------
print "Create and print
a list\
with 3 nested
elements"
listA = [[2,4],[8,16,32],
[64,128,256,512]]
print listA
print "Number of elements
is:"
print len(listA)
print "Length of Element
0 is"
print len(listA[0])
print "Length of Element
1 is"
print len(listA[1])
print "Length of Element
2 is"
print len(listA[2])
D:\Baldwin\AA-School\PyProg>python
Lists07.py
Create and print a list with
3 nested elements
[[2, 4], [8, 16, 32], [64,
128, 256, 512]]
Number of elements is:
3
Length of Element 0 is
2
Length of Element 1 is
3
Length of Element 2 is
4
#
File Lists08.py
# Rev 2/4/00
# Copyright 2000, R.
G. Baldwin
# Illustrates a three-
# dimensional
array list
#
#-------------------------------
print "Create and print
a\
three-dimensional
array list"
listA = [[[[1],[2]],[[3],[4]]],
[[[5],[6]],[[7],[8]]]]
print listA
print "Print each element"
print listA[0][0][0]
print listA[0][0][1]
print listA[0][1][0]
print listA[0][1][1]
print listA[1][0][0]
print listA[1][0][1]
print listA[1][1][0]
print listA[1][1][1]
Program output
D:\Baldwin\AA-School\PyProg>python
Lists08.py
Create and print a three-dimensional
array list
[[[[1], [2]], [[3], [4]]],
[[[5], [6]], [[7], [8]]]]
Print each element
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
The triple-square-bracket notation in the program of Figure 9 is essentially the same notation that would be used to access the individual elements in a three-dimensional array in C, C++, or Java.
More Information on Lists
Review
Answer:
listA
= [100,200,300,400,500]
listA[1:4] = [2,4,8,16,32,64] Figure 11 |
2. Show how to replace an element in a list with a nested list.
Answer:
listA
= [100,200,300,400,500]
listA[2] = [2,4,8,16,32,64] Figure 12 |
3. Show how to extract the elements from one list that is nested in another list.
Answer:
listA
= [100,200,300,400,500]
listA[2] = [2,4,8,16,32,64] print listA[2][0] print listA[2][1] print listA[2][2] print listA[2][3] print listA[2][4] print listA[2][5] Figure 13 |
4. Show how to create a list of nested lists where the nested lists have different lengths.
Answer:
listA
= [[2,4],[8,16,32],
[64,128,256,512]] Figure 14 |
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.