Page 1 of 1

A Quick Overview of Nested Lists in Python

#1 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Posted 04 October 2010 - 05:49 PM

A Quick Overview of Nested Lists in Python

Before we begin, I would first like to define what a List is: A list is exactly what is sounds like. It is a list that is created to hold data, in a given order. A List is one of the Data Structures that is used in the Python language and is similar to an array or an ArrayList in Java, for those of you who already know Java. During this tutorial, I will assume that we all have a basic understanding of Lists.

Nested Lists

The basic idea of a nested list is that you have, essentially, a list made up of lists. For example:

L1 = [1,2]
L2 = [[1,2,3],[4,5,6]]

Now, I believe that most people reading this will know that a computer starts counting at 0, not at 1. So we know that L1[0] the first element in the list, not L1[1]. With that said, let's explore a bit.

From the example that I have provided, we can see that L1[0] is equal to 1, and L1[1] is equal to 2. In a nested list, such as L2, the first element of the List, is a List itself. So, L2[0] is the list of [1,2,3], and L2[1] is the list of [4,5,6]. Understanding this point is vital to understanding my next point.

When asking for a specific element in a List, we say: L1[x], where x is the element desire. But what do we say when we want a specific element from a nested list? We say: L1[x][y]. This is easier to show than it is to explain, so here is an example:

L1 = [ 1, [73,89,42,32], 62, [24, 32], 99 ]

In this example, L1[0] has the value of 1, but L1[1] has the value of [73,89,42,32]. From here, we will act exactly as if we were getting an element from a non-nested list, with only one difference. We are going to add another set of square brackets into the mix. Example:

L1 = [ 1, [73,89,42,32], 62, [24,32], 99 ]
L1[1] = [73,89,42,32]

L1[1][0] = 73
L1[1][1] = 89
L1[1][2] = 42
L1[1][3] = 32

Errors in Nested Lists

Nested lists can go as deep as your list will let it. Meaning that in the above example we can legally say L1[1][0] = 73, but if we try to go deeper, say we try to ask for L1[1][0][0], then we will get an error. We will also get an error if we try to ask for an element that doesn't exist, in a level that does exist. Ex:

L1 = [ 1, [73,89,42,32], 62, [24,32], 99 ]

L1[1][0] = 73 # Good Statement!
L1[3][1] = 32 # Good Statement!

L1[1][0][0] = ? # Bad Statement!
L1[3][3] = ? # Bad Statement!

The first two examples given are perfectly legal. However, the other two examples are illegal and each will throw an error. L1[1][0][0], in this case, would throw what is known as a TypeError. What this means is that the sub-level you requested, does not exist. This will happen whenever you try to go too deep into your list. L1[3][3] is also an illegal statement. The difference between this example and L1[1][0][0] is that this example doesn't go too deep, it goes too far. This will through an IndexError. An IndexError will occur whenever you try to request an index that doesn't exist. In L1[1], the last entry is 32, this is known as L1[1][3]. So, we can't call L1[1][4], because it doesn't exist.


I hope this tutorial was helpful, and if all goes well then I plan to write more in the future. Please let me know what you did or did not like in the tutorial, or if I did not explain something well. I am more than glad to answer any questions that you have!

Is This A Good Question/Topic? 4
  • +

Page 1 of 1