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


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 variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?