<< Chapter < Page | Chapter >> Page > |
Figure 2 . Program output from replacing an element with a list. |
---|
Create and print a list
[100, 200, 300, 400, 500]Original length is:
5Replace an element
Print the modified list[100, 200, [2, 4, 8, 16, 32, 64], 400, 500]Modified length is:
5 |
As you can see from Figure 2 , 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.
The code visualization shown in Figure 3 summarizes the concepts that were explainedin the previous two sections. This image was produced by entering the five statements shown in the code visualizer's code window and stepping through all five instructions.
Figure 3. Visualization.
I recommend that you use the code visualizer to step through this code and observe the state of the program in memory as eachinstruction is executed. Try to correlate the behavior of this program with what you learned in the sections above titled Replacing a slice and Replacing an element with a list .
Now I am going to illustrate the syntax for extracting elements from a nested list using pairs of matching square brackets . Listing 3 is an expansion of Listing 2 .
Listing 3 . Extracting elements from a nested list. |
---|
# 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("Extract and display each")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]) |
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.
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.
The output from this program is shown in Figure 4 .
Figure 4 . Program output from extracting elements from a nested list. |
---|
Create and print a list
[100, 200, 300, 400, 500]Original length is:
5Replace an element
Print the modified list[100, 200, [2, 4, 8, 16, 32, 64], 400, 500]Modified length is:
5Extract and display each
element in the list100
200[2, 4, 8, 16, 32, 64]
400500
Extract and display eachelement in nested list
24
816
3264 |
Notification Switch
Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?