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