declare

2D Lists & Nested Loops - Python - Tutorial 24



Sharing buttons:

hey welcome to draft Academy my name is

Mike and this tutorial I want to talk to

you guys about two different concepts in

Python it's gonna be a pretty cool

lesson first thing I want to show you

guys though are two-dimensional lists so

down here in our Python file I just want

to create a basic list so I'm just gonna

call this number grid and I'm just gonna

set this equal to a normal list right

just like this now normally if we were

creating a list I could just put in a

bunch of attributes and bunch of

elements right and now all these

elements are inside of this number grid

list everybody's happy

but another thing I can do is I can make

all of the elements inside of this

number grid list lists so the first item

in the number grid list is gonna be a

list the second item in the number grid

lists is gonna be a list excetera so I'm

gonna show you guys how to do that I can

basically just come down here and when I

would normally put the first element in

the list I can just create another array

so I'm just gonna make an opening close

square bracket and now I'm just gonna

say 1 2 3 and I can create the next item

in my list I can say 4 5 6 and let's

make another item in this list I can say

7 8 9 and finally we're gonna put one

more list inside of the number grid list

and it's just gonna have zero in it so

inside of this number grid list right

this high-level overall list we have

four elements and all of those elements

are themselves lists so essentially what

we're doing is we're creating a grid

right this grid has one two three four

rows and one two three columns right

four rows three columns and that's kind

of like why these can be useful is we

can create a grid like structure inside

of Python using two-dimensional lists so

this is a pretty cool list structure and

I want to show you guys how it can

access individual elements inside of

this list structure so for example let's

say I wanted to print

one of these values let's say I wanted

to print out this one right here the way

I can access it is by saying number grid

and the first thing I want to do is make

an open and close square brackets and in

here I want to put the index of the row

that I want to access so this would be

like row zero because that's the zeroth

element in the array this would be Row 1

it's at index position 1 this would be

Row 2 Row 3 etc so I could put row 0 and

now right next to this I'm gonna make

another square bracket and I want to put

the index of the column so this would be

like column 1 column 2 column 3 so I'm

gonna put another 0 because this is at

the 0th index of this first array so I

put 0 0 here now we can print this guy

out to the screen and you'll see down

here that we get that 1 so I could also

do this for like for this 8 for example

so this is going to be 0 1 2 it's gonna

be row 2 and column 1 and now we should

be able to print out that 8 so that's

basically how we can access elements

inside of this 2d list the next thing I

want to show you guys is a nested for

loop nested for loop is a situation like

I said where we have a for loop inside

of a for loop and I'll show you how we

can use this nested for loop in order to

print out all the elements inside of

this array so I basically show you guys

how to parse through a two-dimensional

list or a two-dimensional array alright

so basically we're just gonna create a

normal for loop so I'm gonna say 4 and

I'm just gonna say row in number grid

and I'm using the word row here because

I'm kind of seeing these guys as rows

right these are sort of horizontal rows

and our little grid so for each of those

rows for each of the elements inside of

number grid I want to loop through so I

could actually just print out each row

right now and it's just gonna print out

all the rows inside of this list so you

can see down here we basically just get

exactly what we have up there but I want

to be able to access each of these

individually

attributes inside of each of these array

elements so we can actually create

another for loop and I'm gonna put that

for loop inside of here so now I'm gonna

say for column in row and basically what

this is going to give us is each

individual column or each individual

element inside of these arrays so for

example inside of the arrays that are

the elements of the number grid

hopefully that makes sense so down here

now I can just print out column and this

should actually print out every single

value inside of this two-dimensional

array so let's go ahead and run this

program and down here you'll see that

we're basically printing out one all the

way down to zero so we're printing out

all of the elements inside of all of the

arrays inside of the number grid so

that's how we can use two-dimensional

lists and nested for-loops

together and both of those things are

actually pretty handy in Python hey

thanks for watching if you enjoyed the

video please leave a like and subscribe

to drop acad to be the first to know

when we release new content also we're

always looking to improve so if you have

any constructive criticism or questions

or anything leave a comment below

finally if you're enjoying chopped

Academy and you want to help us grow

head over to draft Kadim e-comm forward

slash contribute and invest in our

future