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

Miscellaneous

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

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.

-end-

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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