<< Chapter < Page Chapter >> Page >

More on nested elements

The program in Listing 4 illustrates some additional aspects of nested elements.

Listing 4 . More nested elements.
# 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]))

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 5 . The program displays the entire list, and then gets and displays the lengths of each of the nested lists.

Figure 5 . Output from more nested elements.
Create and print a list with 3 nested elements [[2, 4], [8, 16, 32], [64, 128, 256, 512]]Number of elements is: 3Length of Element 0 is 2Length of Element 1 is 3Length of Element 2 is 4

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, you may want to 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

The program in Listing 5 might represent what an experienced programmer would consider to be athree-dimensional array of integer data in some other programming environment.

Listing 5 . A three-dimensional array program.
# 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])

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. This is very similar to thesyntax in other programming languages.

Visualization of a three-dimensional array

Figure 6 shows a visualization of the three-dimensional array pointed to by the variable named listA in Listing 5 . In programming, we deal with arrays having one, two, three or more dimensions. As you can see, even a three-dimensional array is arelatively complex structure.

Figure 6. Visualization of a three-dimensional array.

Visualization of a three-dimensional array.

Program behavior

This program

  • Creates and populates the list that represents a three-dimensional array.
  • Displays the entire array as a set of nested lists.
  • Displays the contents of each element in the array.

Output from a three-dimensional array program

The output from the program is shown in Figure 7 . The triple-square-bracket notation in the program of Listing 5 is essentially the same notation that would be used to access the individual elements in athree-dimensional array in C, C++, or Java.

Figure 7 . Output from a three-dimensional array program.
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]

More information on lists

There is quite a lot more that you will need to learn about lists. However, I will defer that discussion for a future module where I discuss the use of a listas a data structure or a container .


This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Itse1359-1090-Lists Part 2
  • File: Itse1359-1090.htm
  • Published: 10/15/14
  • Revised: 01/31/16

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.


Questions & Answers

how can chip be made from sand
Eke Reply
are nano particles real
Missy Reply
Hello, if I study Physics teacher in bachelor, can I study Nanotechnology in master?
Lale Reply
no can't
where we get a research paper on Nano chemistry....?
Maira Reply
nanopartical of organic/inorganic / physical chemistry , pdf / thesis / review
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
has a lot of application modern world
what is variations in raman spectra for nanomaterials
Jyoti Reply
ya I also want to know the raman spectra
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
Nasa has use it in the 60's, copper as water purification in the moon travel.
nanocopper obvius
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
analytical skills graphene is prepared to kill any type viruses .
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
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
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 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?