2D Lists

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/7

encourage image

There's no tags or description

Looks like no tags are added yet.

8 Terms

1
New cards

Say you have the list:

my_2d_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

What would each of these print?

print(my_2d_list[0][0])
print(my_2d_list[2][0])
print(my_2d_list[1][2])
print(my_2d_list[2][3])


  1. Print 1

  2. Print 7

  3. Print 6

  4. Error (out of range for [3])


2
New cards

What about this? (Pay attention to brackets)

my_3d_list = [[[1,2,3], [4,5,6], [7,8,9]], [[1,2,3], [4,5,6], [7,8,9]], [[1,2,3], [4,5,6], [7,8,9]]] 
print(my_3d_list[0][0][0])
print(my_3d_list[0][1][2])
print(my_3d_list[1][2][3])


  1. Print 1

  2. Print 6

  3. Error ([3] is out of range)


3
New cards
<p>Good for understanding (flip card for more facts)</p>

Good for understanding (flip card for more facts)

So if we had something like weird_list[1][2]…

This would select row 1, which is [4, 5, 6] and then go to column 2 which is element 6!

<p>So if we had something like weird_list[1][2]…</p><p>This would select row 1, which is [4, 5, 6] and then go to column 2 which is element 6!</p>
4
New cards

What does this result in?

board = []
for x in range(8):
    board.append([])
    for y in range(8):
        board[-1].append("")


for x in range(8) means your going thru 0-7 and each time, you add a list into board.

So the first iteration of x = 0 in the range will give:

[[ ]]

Then you go into the next for loop, which is the same range for 0-7. However, this one will run all 8 times before moving to the next iteration.

Board[-1] accesses the last index in the list, which is gonna be the inner list that we just created, bc that’s the only thing in it rn. So doing board[-1].append(““) is going to add ““ inside that inner list.

So you should have [[““]] (for y = 0). But then it keeps going, so on the next one y=1, it’s gonna append ““ to the end of whatever element is in our inner list.

So now we have [[““, ““]]. Bc the range is 8, it’s gonna do that 8 times, so from 0-7.

At the end of the first iteration we should have:

[[““, ““, ““, ““, ““, ““, ““, ““]]

The next iteration will result in the same thing but on the next line. So at the end of all iterations we should have.

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

[[““, ““, ““, ““, ““, ““, ““, ““]]

5
New cards

What would this result in?

board = []
row = []
for y in range(8):
    row.append("*")
for x in range(8):
    board.append(row[:])


for y loop after first iteration you have:

[“*”]

For each iteration another * is added to the list. So at the end you should have

["*", "*", "*", "*", "*", "*", "*", "*"]

Now the for x loop, board is already a list. Remember [:] creates a copy of the entire list. So row[:] creates a copy of the final row list after the first for loop. Now when it says board.append(row[:]) it means you’re adding that final row list within the board list. Bc it is iterating 8 times, there should be 8 elements of the row list in the board list.

After first iteration you have:

[["*", "*", "*", "*", "*", "*", "*", "*"]]

Second iteration you have:

[

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"]

]

So at the end of the loop you should have:

[

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"],

["*", "*", "*", "*", "*", "*", "*", "*"]

]

6
New cards

How do you access an element in a perfectly square 2D list?

Perfectly square 2D list is when each inner list has the same number of elements. For example, like:

my_2d_list = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]


if 0 <= outer_index < len(weird_list):
    print(weird_list[outer_index][inner_index])

You just check if the outer list exists by checking if the outer index exists! Since it’s a perfectly square 2d list you don’t need to check if the inner list exists, bc every inner list should have to same amount of elements!

  • outer_index >= 0 makes sure the index isn't negative (so if the inner list exists)

  • outer_index < len(weird_list) makes sure the index isn't too large.


7
New cards

How do you access an element in a 2D list that isn’t perfectly square?

For example, if you have different lengths of lists like this…

weird_list = [
    [1, 2, 3],
    [1, 2, 3, 4, 5, 6],
    [],
    [2, 3, 4, 5]
]


if 0 <= outer_index < len(weird_list) and 0 <= inner_index < len(weird_list[outer_index]):

Bc every list has different elements, checking the overall length like we did with a perfect square list isn’t enough.

The first if statement checks whether the outer list exists. Since weird list has 4 rows, the valid indexes are 0, 1, 2, 3.

The second if statement checks whether the inner list exists. (columns exist in the specific row the user selected). For example, if outer_index = 1, then len(weird_list[1]) = 6 because the length of that row is 6 (it has 6 elements). So the indexes would be from 0 to 5.

8
New cards

How do you change an element in a 2D list?

You locate it, then assign it. Like this

my_2d_list[0][2] = 100
my_2d_list = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

So now our list looks like:

[
    [1, 2, 100],
    [4, 5, 6],
    [7, 8, 9]
]