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

is economics important in programming world?
Abdul Reply
What is oppunity cost
Kudzanayi Reply
it refers wants that are left unsatisfied in Oder satisfy another more pressing need
Thank bro
is the second altenative to foregone
How does monopoly and imperfect competion;public goods externalities ass symmetric information, ommon property ressourses; income distribution Merit goods and Macroeconomic growth and stability causes market failure?
it is where by a labour moves from one place to another
Amoako Reply
what is mobility of labour
George Reply
who best defined economics
paul Reply
what are the importance of economics
Adwoa Reply
trade should be best
help to know how our government operates in which system
1. To solve economic problems. 2. To predict economic event. 3. It also offer intellectual training to students.
How price elasticity can affect the markets of certain goods
Rhoda Reply
Heyy thanks for the teaching
Easter Reply
what is labour
Arthur Reply
Mental and physical ability of human being is considered as labour.
and usually provided by human
is all human effort both physical and mental abilities to work
why is it that most countries in Africa abuse available resources
Okeoghene Reply
What is economics
Bright Reply
economics is a science which studies human behaviour as a relationship between ends, scarce means which have alternative uses.
Why is scarcity a mind problem in economics
Alima Reply
because of the problem of allocation of resources
unequalled distribution of resources
scarcity is a mind problem due to circumstances like when a particular product is demand in a high rate at the market.
scarcity defines limited in supply relative to the demand them. so scarcity is a mind problem in economics because wants are unlimited while resources are also limited.
it is a mind problem because it's one of the fundamental issue address by economics human wants are unlimited and resources available are limited this makes the study of economics essential
its a fundamental issue
it's not a mind problem, I think it's a economic problem, how to allocate scarce resources to satisfy need and want of society
Identify the different sectors in the economy
Moses Reply
what is economics
economic is study of human behaviour according to how they satisfie their numerous want
Economics is a science which studies human behaviour as a relationship between ends and scares resources.
economic is study of human behaviour according to how they satisfie their numerous want
what is the law of demand
Adugbire Reply
the lower the price the higher the quantity demanded vice versa is true
yes ooh
what are relationship between unemployment and economic growth
Weness Reply
no relationship
When there is economic growth, there is increased opportunity in employment. When there is no economic growth mean there is recession causing a decline or downsizing in employment opportunities.
unemployment or employment determined by the level of economic growth
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Leah Reply
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?