<< 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

One element is itself a list

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.

A visualization

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.

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 .

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 . 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])

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 from extracting elements from a nested 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

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask