themselves can be lists!
In a multidimensional list, more than one index value is associated with each specific data element
contained in the multidimensional list. It can get somewhat complicated trying to keep track of your
data in multidimensional lists, but these lists do come in handy!
You create a multidimensional list the same way you create normal lists, just with defining lists as the
data values. Here’s an example:
Click here to view code image
>>> list13 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print(list13)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>
To reference an individual data value within a multidimensional list, you must specify the index value
for the main list, as well as the index value for the data value list. You place square brackets around
each index value and place them in order from the outermost list to the innermost list. Here are some
examples:
>>> print(list13[0][0])
1
>>> print(list13[0][2])
3
>>> print(list13[2][1])
8
>>>
The first example retrieves the first data value contained in the first list. The last example retrieves
the second data value contained in the third list. This demonstrates a two-dimensional list. You can
continue this further by using list data values for the list data values within the list, creating a three-
dimensional list! You can continue on even further, but anything more than three dimensions starts
getting extremely complicated.
Working with Lists and Tuples in Your Scripts
Lists and tuples are powerful tools to have at your disposal in your Python scripts. Once you load
your data into a list or tuple, there are lots of Python functions you can use to extract information on
the data. That can make having to perform mathematical calculations a lot easier!
The following sections show some of the most common data functions you can use with your lists and
tuples.
Iterating Through a List or Tuple
One of the most popular uses of lists and tuples is iterating through individual items using a loop.
When you do this, you can grab each data value contained in the list or tuple individually and process
the data.
To iterate through the data values, you need to use the for statement (discussed in Hour 7, “Learning
About Loops”), like this:
Click here to view code image
>>> list14 = [1, 15, 46, 79, 123, 427]
>>> for x in list14: