FAQ: Introduction to Lists in Python - Modifying 2D Lists

This community-built FAQ covers the “Modifying 2D Lists” exercise from the lesson “Introduction to Lists in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Modifying 2D Lists

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi,
This was a case in which the code runs, even though it was not the correct answer.
`"Kenny"` likes to be called by his nickname `"Ken"` . Modify the list using double brackets `[][]` to accommodate the change but only using negative indices .

incoming_class[-3][-3] = “Ken”
incoming_class[-3[0] = “Ken”

The code run either way but only one solution is acceptable.
Out of curiosity: Is there a practical reason for me to not mix positive with negative indexes in a situation like this?

Many thanks!

I was wondering if there was a way to modify both values attributed to an element of a 2D list at the same time

For example:

List = [[“Mario”, “Player 1”], [“Luigi”, “Player 2”]]

Would there be a way to change [“Mario”, “Player 1”] to [“Bowser”, “Boss”] with a single line?

1 Like

I think that mixing positive and negative indexes will make bug-checking a bit difficult, hence it is best to stick with positive or negative values when changing one element of the list.

As for why the second version is incorrect, the exercise asked to only use negative index values… as zero is neither positive or negative, you likely got flagged for not meeting the expected answer…

1 Like

Yes, you’re right. It’s not only the outcome that matters. We need to follow the exact instructions.

player_list = [[‘Mario’, ‘Player 1’], [‘Luigi’, ‘Player 2’]]

player_list[0] = [‘Bowser’, ‘Boss’]
Just don’t go to the second list dimension
player_list[0], player_list[1][0] = [‘Bowser’, ‘Boss’], [‘Todd’]
You can also modify more than one list and/or values in the same line using the comma.

Not sure you still need this, I forgot to watch my notifications.

Hi everyone! I’m new to python and my question about list is simple

Right now we have indexes

[“Jenny”, “sam”, “ronny”]
Jenny: 0
Sam: 1
Ronny: 2

or
score = some_list[2][1]
then print(score)

Finding and also modifying the int or float values associated with the string or finding/modifying a string associated to a other string like [“Max”, “Golf”], but what if the list had a 1000 elements? is there a way to find and modify elements? we won’t be able to find the indexes manually

Welcome to the forums!

Before I get to manipulating elements in large lists, there’s one thing I want to point out.

This doesn’t quite do what you think it does. `some_list` is a one-dimensional list, and you access one of its elements like so: `some_list[index]` where `index` is an integer between 0 and one less than the length of the list, inclusive. The notation `some_list[index1][index2]` is used to access elements in a two-dimensional array (list).

There are many ways that you can accomplish this. Here’s just one of many possible options. The full list of list methods from the docs is here. If we access a list element using `some_list[index]`, all we have to do is find the index of the element we need. Take a look at the methods from the link above and see if you can figure out how you can find the correct index. Hint: Use the `.index()` method.

1 Like

Thanks for such a detailed response I think I got it now, and yes score = some_list[2][1] was a 1D list, I’m studying loops now

age_list = [[“Jenny, 19]”, [“sam”, 31]”, [“ronny”, 20]…[“Alex”, 23]…[“name”, n]]
#let us suppose list has over 500 elements
find_index = age_list.index()
print(find_index)

Will this help me in finding a particular or random index value in the list if the list had over 500 names with ages?

For example Alex is the 350th element and someone wants his/her info?

1 Like

There is one drawback to the `.index()` method… It only finds the index of the first target match so assumes all the elements are unique. The probability that in a list of 500 names at least two are the same is more than zero. Feasibly, closer to 1 than 0.

If you are learning loops, the first one would be to enumerate your list (give it physical indices paired up with the data point).

``````>>> age_list = [["Jenny", 19], ["Sam", 31], ["Ronny", 20],["Alex", 23],["Jenny", 21]]
>>> enum_age_list = []
>>> for i in range(len(age_list)):
enum_age_list.append([i] + age_list[i])

>>> enum_age_list
[[0, 'Jenny', 19], [1, 'Sam', 31], [2, 'Ronny', 20], [3, 'Alex', 23], [4, 'Jenny', 21]]
>>>
``````

The next step would be find the target name with another loop.

``````>>> target = 'Jenny'
>>> matches = []
>>> for x in enum_age_list:
if target == x[1]:
matches.append(x)

>>> matches
[[0, 'Jenny', 19], [4, 'Jenny', 21]]
>>>
``````

Now we have a collection, each with the index where they appear in the `age_list`.

1 Like

Extra Study

List Comprehensions

``````>>> x = age_list
>>> [y for y in [[i] + x[i] for i in range(len(x))] if target == y[1]]
[[0, 'Jenny', 19], [4, 'Jenny', 21]]
>>>
``````

This is a step up the ladder, but looping (iteration) is central.

Compare the loops above to these ones. Comprehensions are iterables. Above they are nested.

Sometimes it helps to look at a problem in a different language to get a feel for the situation:

``````const age_list = [["Jenny", 19], ["Sam", 31], ["Ronny", 20],["Alex", 23],["Jenny", 21]]
const enum_age_list = []
age_list.forEach(function (x, i) {
x.unshift(i)
this.push(x)
}, enum_age_list);
let target = 'Jenny'
const matches = []
for (let x of enum_age_list){
if (target === x[1]) matches.push(x)
}
console.log(matches)
``````
``````[ [ 0, 'Jenny', 19 ], [ 4, 'Jenny', 21 ] ]
``````

That’s JavaScript.

Hi,
When modifying 2D lists that contains 3 item in each list, what should we do?
Our school is expanding! We are welcoming a new set of students today from all over the world.

Using the provided table, create a two-dimensional list called `incoming_class` to represent the data. Each sublist in `incoming_class` should contain the name, nationality, and grade for a single student.

`"Kenny"` `"American"` 9
`"Tanya"` `"Russian"` 9
`"Madison"` `"Indian"` 7

1, Print `incoming_class` to see our list.
2, `"Madison"` passed an exam to advance a grade. She will be pushed into 8th grade rather than her current 7th in our list.
Modify the list using double brackets `[][]` to make the change. Use positive inidices .
Print `incoming_class` to see our change.

*****I have question with the second question. In this case, every list has three items, and if I want to change Madison’s grade, should I write incoming_class[2][2][2] = 8? However, After I run the code, it shows “TypeError: ‘int’ object does not support item assignment”, and I check the correct way to write it is incoming_class[2][2] = 8. Why is that?

incoming_class = [[“Kenny”, “American”, 9], [“Tanya”, “Russian”, 9], [“Madison”, “Indian”, 7]]

print(incoming_class)

incoming_class[2][2][2] = 8

print(incoming_class)

What you have there is to access a 3D list. `class[2][2]` will access the grade of the third member of the class (grade is also the third value of its respective list)…

3 Likes

Hi mtf,
Thank you replying me! Now I understood. In the first incoming_class [2] I can find the 3rd list in the incoming_class list which is the [“Madison”, “Indian”, 7], and in the second [2], I can find 3rd item which is 7 in the [“Madison”, “Indian”, 7]. Thank you for teaching me how to double brackets in a 3D list.

2 Likes

You’re welcome. To reiterate, this is a 2D list, not 3D.

``````incoming_class = [
[“Kenny”, “American”, 9],
[“Tanya”, “Russian”, 9],
]
``````

In a 3D list, each row would be a list of lists, just as `incoming_class` is a list of lists.

2 Likes

Oh, Ok. Thank you so much for the information!

1 Like

Essentially it reads from left to right every time it enters. So it reads 0,1,2 to get to the third list then reads 0,1,2 to get to the third item within the list hence [2][2].

2 Likes

In the 2D list modifying how is accessing incoming_list[2][2] only changing one value when there are three values listed?

Am I correct in the understanding that inside of the brackets each parameter has an associated number [“ken”, “Frank”, 10] break down to 0, 0, 0 if they are the first in the list. Followed by 1, 1, 1?

Having two subscripts in the form of `[0][2]` first subscripts the object on the left by `[0]` and then uses another subscript `[2]` on whatever was returned.

Take the example given in the instructions:

``````class_name_hobbies = [
["Jenny", "Breakdancing"],
["Alexus", "Photography"],
["Grace", "Soccer"]
]
``````

If we use a single subscript we get the first element of `class_name_hobbies` which is itself a list.

``````print(class_name_hobbies[0])
Out: ['Jenny', 'Breakdancing']
``````

If we were to assign a new name to this object-

``````temp = class_name_hobbies[0]
``````

then we could access the elements of that list using another subscript, like-

``````print(temp[0])
Out: Jenny
print(temp[2])
Out: Breakdancing
``````

But there’s no need for another assignment, we can just subscript the inner list by using a second subscript. Note that the leftmost subscript is used first. So using `temp[1]` here is the same as using `class_name_hobbies[0][1]`-

``````print(class_name_hobbies[0][0])
Out: Jenny
print(class_name_hobbies[0][1])
Out: Breakdancing
``````

So `class_name_hobbies[0][1]` is much the same as getting the object at `class_name_hobbies[0]` then indexing it again with `[1]`.

Try playing around with the indexing a little as it may be easier when you test it out for yourself.

2 Likes