Learn to Program using Python

Lists, Part II

By Richard G. Baldwin

Python Programming Lesson #32

August 1, 2000


This document is part of a series of online tutorial lessons designed to teach you how to program using the Python scripting language.

Something for everyone

Beginners start at the beginning, and experienced programmers jump in further along. Lesson 1 provides an overall description of this online programming course.


A previous lesson introduced you to lists.

Plus some other structures

It also introduced you to subscriptions, sequences, mutable sequences, mappings, slicings, and tuples.

Manipulating lists

That lesson showed you some of the ways that you can manipulate lists.  The discussion was illustrated using sample programs.

Other ways to manipulate lists

This lesson carries that discussion forward by using sample programs to teach you other ways to manipulate lists.

Some Sample Programs

Replacing a slice

You can replace a slice in a list with the elements from another list through assignment.

Can change the length of the list

Note that this operation can change the length of the original list.

Replacing a slice in a list with the elements from another list is illustrated in Figure 1.

The function named len()

This program also illustrates the use of the function named len() to get and print the length of the list.

Replaces a three-element slice with a six-element list

In this program, a slice of an original five-element list, consisting of the elements from 1 through 3 inclusive, is replaced by the elements of a new list consisting of six new elements.

Length of the list is increased by 3

Since three existing elements are replaced by six new elements, the overall length of the list is increased by three elements.

Program output

The output from this program is shown in Figure 2 (boldface added for emphasis).

Six new elements replaced three original elements

As you can see, the six new elements replaced the three original elements to increase the length of the list from 5 to 8 elements.

Replacing an element with a list

It is also possible to replace an element in an existing list with a new list, as illustrated in the following program.

Behavior is different from above

In this case, the behavior is different from that shown above where a slice from the original list was replaced with the elements from a different list (even though the right operand of the assignment operator is the same in both cases).

Produces nested lists

When a single element is replaced by a list, the result is that a new list is nested inside the original list.

Length is unchanged

It is also interesting to note that the length of the list is unchanged by this operation since the list that replaces the element is itself considered to be a single element.  Therefore, the number of elements is not changed.

Sample program

This is illustrated in Figure 3, where the element at index 2 of an original list is replaced with a new list having six elements.

Program output

The output from this program is shown in Figure 4 (boldface added for emphasis).

One element is itself a list

As you can see, 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.

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. Figure 5 is an expansion of Figure 3.

Note the boldface statements

The most interesting statements in this program are highlighted in boldface.

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

The output from this program is shown in Figure 6 (boldface added for emphasis).

More on nested elements

The program in Figure 7 illustrates some additional aspects of nested elements.

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 8 (boldface added for emphasis).  The program displays the entire list, and then gets and displays the lengths of each of the nested lists.

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

In any event, the program in Figure 9 might represent what an experienced programmer would consider to be a three-dimensional array of integer data in some other programming environment.

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.

Program behavior

This program

Program output

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

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 lesson where I discuss the use of a list as a data structure or a container.


1.  Show how to replace a slice of a list with the elements from a new list.

Ans:  See Figure 11.

2.  Show how to replace an element in a list with a nested list.

Ans:  See Figure 12.

3.  Show how to extract the elements from one list that is nested in another list.

Ans:  See Figure 13.

4.  Show how to create a list of nested lists where the nested lists have different lengths.

Ans:  See Figure 14.

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.