2️⃣

10.2 Retrieving and Updating Elements

Retrieving and Updating Elements

You retrieve and update elements in a multi-dimensional list the same way you would for 1D lists, except now you have to deal with multiple levels of indices. The table below shows how you would retrieve each value in my_list. Do you see any patterns?
 

0

1

2

3
0

my_list[0][0]

my_list[1][0]

my_list[2][0]

my_list[3][0]
1

my_list[0][1]

my_list[1][1]

my_list[2][1]

my_list[3][1]
2

my_list[0][2]

my_list[1][2]

my_list[2][2]

my_list[3][2]
You can also think about multi-dimensional lists this way:
The first index is the first "level" of the list. The second index is the second "level" of the list. It's list-ception!
 

Example

Let's say we're trying to retrieve my_list[1][2]. How would we get there?
First, look at the first level. We need to retrieve the element at index 1. That would be
my_list index

1

['docs', 'slides', 'sheets']
2

['google', 'amazon', 'facebook']
3

['this', 'is', 'python']
Now we can go a level deeper. The next index is 2, so that would be:
my_list[1] index

0

'docs'
1

'slides'
2

'sheets'
Therefore, my_list[1][2] is 'sheets'!
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.