- [Instructor] In this video,
I'd like to just do a brief demonstration
of two dimensional lists.
So, in Python
the way we represent those is with a list
that contains as its elements other lists.
Now, it turns out
that most of the time you're working
with two-dimensional data structures,
or potentially higher dimensional data structures.
You're typically going to work with data structures
that are more efficient than lists are in Python.
So, a couple of lessons from now
we're going to take a look
at a library for high performance computing,
called NumPy, Numerical Python
which provides an array data structure
that can be used for one-dimensional arrays,
two-dimensional arrays,
and higher dimensional arrays as well.
And we'll also take a look
at a popular library used in data science
and artificial intelligence called Pandas,
which provides both one-dimensional
and two-dimensional data structures.
Both of those libraries are used more frequently
than just plain old every day two-dimensional lists,
for maintaining two-dimensional data.
Now, let me go ahead and paste in
a snippet of code here
that will create a two-dimensional list.
In Python,
a two-dimensional list again is just a list of lists.
So, what I have here is a nested list structure.
The outermost square brackets define a list
and that lists elements are a sub-list,
a second sub-list
and a third sub-list.
So, each of those sub-list
represents a row in a table of values
and then within each row,
we have in this case four columns.
The first row contains the value 77, 68, 86 and 73.
The second row contains the next four values listed here,
and the third row contains
the last four values listed here.
Now, if I go ahead and create that,
that's going to now create the list in memory
and conceptually that list is going
to look something like this.
Now, this is just conceptual.
The actual memory layout
is going to be a list object,
that contains references to other list objects.
But, in row zero
we have the first four values
that were defined in the first sub-list.
In row one,
we have the values that were defined in the second sub-list,
and in row two we have the values
that were defined in the third sub-list.
And by the way,
there is nothing that says that anyone of those rows
has to have the same number of columns.
You can have what we call jagged or ragged arrays
in some programming languages,
where each row could in fact have a different
number of elements as well.
Now, when you want to identify the elements...
Whoops, I wanted to go to the next slide there.
When you want to identify the elements,
when you have one set
of square brackets following a variable name,
that normally is for a one-dimensional list.
But, if you have multi-dimensional lists,
then you use two sets of square brackets
where the first one is the row
and the second one is the column.
So, in row zero the first dimensions subscript,
or index is always going to be zero
and then the columns will be
zero, one, two and three respectively.
And this is the same as it is
in just about every C based programming language out there.
So, let's see how we can use a nested for loop
to process a two-dimensional list
and display its contents.
So, let me copy and paste a loop structure in here.
So, here we have a nested for loop
and in the first outer for loop here,
we've defined a target variable called Row,
that says for each of the rows
in the two-dimensional lists A.
What it's really saying is for each element in the list A,
those elements happen to be themselves other lists.
But, it's really just for each element in the list A,
I wanna do something.
And because each of those elements is itself a list,
we can then iterate through that row
as we called it based on the variable up above.
And for each item in that row,
we would then like to output the item
followed by a space.
Now, when we complete a row
by finishing off the nested loop,
we then just do an empty print statement,
which moves the Output Cursor
to the beginning of the next line.
And here we can get a two-dimensional representation
of what that array looks like.
And by the way,
when we get to Pandas later on a couple of lessons from now,
one of the really cool things about that library
is that it's capable of automatically formatting data
in two-dimensions like this,
and not only that,
you can give the columns names,
you can give the rows names
separate from just the numeric index numbers as well,
and lots of other cool things
that you'll be able to do with that library.
Now, if you're wondering the order in which
these items will be visited,
we can use another loop to help us figure that out.
So, you may recall previously we introduced
the Enumerate built in function from Python,
which when you give it a sequence
it gives you back Tuples
containing index numbers and values from that sequence.
Well, A is a list of lists,
so when we enumerate A,
what we get are the index numbers
that represent those lists
and each item is a list itself.
The row as far as
our two-dimensional data structure is concerned here.
So, for each Index I and corresponding row,
we would like to go ahead and enumerate that row
and for each Index J and the corresponding item,
we will display A Sub I, Sub J,
so that we can see what it's actual index values are,
followed by an equal sign and the item and then a space.
So, at the end of a given row,
I'll put a new line character as well.
So, this is the output
and we can see that first we output
all the elements in row zero,
then row one,
then row two as you would expect.