1.27 Itse1359-1090-lists part 2  (Page 3/4)

 Page 3 / 4
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```

I only see partial conversation and what's the question here!
what about nanotechnology for water purification
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
what is the stm
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
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
Rafiq
what is Nano technology ?
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
why we need to study biomolecules, molecular biology in nanotechnology?
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
why?
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
what does nano mean?
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
it is a goid question and i want to know the answer as well
Maciej
Abigail
for teaching engĺish at school how nano technology help us
Anassong
How can I make nanorobot?
Lily
how did you get the value of 2000N.What calculations are needed to arrive at it
Privacy Information Security Software Version 1.1a
Good
Berger describes sociologists as concerned with
Got questions? Join the online conversation and get instant answers!