Introduction

Now it's time to take lists to a new dimension. No, you don't need to learn how to code in "The Upside Down," but there are additional degrees of complexity to lists. Lists are used to hold data, but they are also used to categorize it. Values inside a list can be further broken down into other sets. That's essentially what a multidimensional list is.

Two Dimensional Lists

What is a list that holds lists? That's all a two dimensional list is. The list below consists of three lists. Each of the three lists has five elements. Don't worry about numbers quite yet. Just focus on the top level elements, the lists. You can access them the way you would any element in a normal list.
number_sets = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
print(number_sets[1])
The second list of numbers will print out. In order to access those lower elements, you need to use a second set of square brackets. In the first set of square brackets, specifies the top level element that you want to access.

In the example below, that is the first list. Then, in the second set of brackets, specifies the element within that initial top level element that you want. In that same example, it's the second element, or the number "4."
number_sets = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
print(number_sets[0][1])
It's just as easy to access any of the other elements of either to top level lists or the numbers within. Multidimensional lists behave just like regular, single dimensional, lists. Anything that is true of a single dimensional list also applies here.

Three Dimensional Lists

There can be more than one additional dimension to lists. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. In a three dimensional list, there is a list which contains a number of lists, each of which also contains a number of lists.
number_sets = [[[1, 2, 3, 4],[5, 6, 7, 8,],[9, 10, 11, 12],], [[13, 14, 15, 16],[17, 18, 19, 20],[21, 22, 23, 24]], [[25, 26, 27, 28], [29, 30, 31, 32], [33, 34, 35, 36]]]
print(number_sets[0][1][2])
The example above is a three dimensional list. It consists of a list of three lists. Each of those three lists also consists of three lists of four numbers. The numbers of elements in the lists have nothing to do with which dimension the list is. A three dimensional list only means that there are three levels of lists.

You can have a three dimensional list consisting of one list holding a single list which also holds only a single list. You can access an element at the lowest level by specifying indexes in each of the three sets of square brackets. In this case, it's the third number from the second list within the first list.
number_sets = [[[[1, 2],[3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], [[[13, 14], [15, 16]], [[17, 18], [19, 20]], [[21, 22], [23, 24]]], [[[25, 26], [27, 28]], [[29, 30], [31, 32]], [[33, 34], 
[35, 36]]]]
print(number_sets[0][1][1][1])
Yes. That Lovecraftian monstrosity is a real thing. They can actually get more complex than that. Really, it's just here to illustrate a point. You can also see that as more dimensions are added, so are sets of square brackets to access elements. Multidimensional lists can be complex, and can theoretically go on for quite a while, and even that can be extended by using external libraries.

Multidimensional Lists and Methods

Remember where it said that anything that's true for single dimensional lists is also true for multidimensional ones? Well, that goes for methods too. Here are a few examples of using methods on the two dimensional list from before.
number_sets = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
number_sets.append([5, 10, 15, 20, 25])
print(number_sets)
Python had no problem appending the new list of number on.

Methods work for elements within elements as well. As long as the element is properly accessed, it can be used, regardless of its position.
number_sets = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
number_sets[1].pop(3)
print(number_sets)
Because the first element in number_sets is itself a list, pop() can be called on it, just like any other list.

It doesn't matter which methods are called or where. You can still combine methods and modify multiple different parts of the list.
number_sets = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
number_sets[2].reverse()
number_sets[1].append(18)
number_sets[0].extend([12, 14, 16, 18])
print(number_sets)
You probably get the idea. Each list is more-or-less independent, aside from being part of the same list. Feel free to work with them as though they are.

Conclusion

If you've never seen multidimensional lists before, your head's probably spinning right about now. It's a lot to take in, and even worse to try to visualize. Up until four dimensional lists, they can be drawn to correspond with physical dimensions.

A single dimension list is simply a line. If you draw lists dropping down from that line in a two dimensional list it becomes a rectangle or a plane. Then, if you draw lists coming out the back of the drop down lists, it forms a sort of cube. That's still sort of difficult to picture, but it helps some people to think of it that way.

In any case, it's a good idea to get some practice here with these. If you're struggling, don't worry too much. Chances are, you won't see anything beyond the odd two dimensional list for some time, and even those tend to be rare for beginners.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions