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

I just finished the task for “Accessing 2D lists” and I find it difficult to understand how the index count works.

For example, this list:

``````class_name_test = [["Jenny", 90], ["Alexus", 85.5], ["Sam", 83], ["Ellie", 101.5]]
``````

When I was asked to pull the data of Sam (name, score) I wrote the following:

``````sams_score = class_name_test[4][5]
``````

I did so because I counted “Jenny” as 0, 90 as 1 and so on.

I understand, from seeing the solution that my count is wrong, but I don’t understand the logic behind the correct count.

`class_name_test` is a list. It has 4 elements and every element also happens to be a list itself.

``````class_name_test[0]  ------> ["Jenny", 90]
class_name_test[1]  ------> ["Alexus", 85.5]
class_name_test[2]  ------> ["Sam", 83]
class_name_test[3]  ------> ["Ellie", 101.5]

class_name_test[3][0]  ------> "Ellie"
class_name_test[3][1]  ------> 101.5
``````

Furthermore, consider the following example:

``````myList = [2, "Hello", [84, 33], False, [91, "Snow", True], 66]

print(myList[0]) ------> 2
print(myList[1]) ------> "Hello"
print(myList[2]) ------> [84, 33]
print(myList[3]) ------> False
print(myList[4]) ------> [91, "Snow", True]
print(myList[5]) ------> 66

print(myList[2][1]) ------> 33  (since myList[2] is a list)
print(myList[4][2]) ------> True (since myList[4] is a list)
print(myList[1][1]) ------> "e" (myList[1] is a string, myList[1][1] is 2nd character)

print(myList[0][0]) ------> TypeError: 'int' object is not subscriptable
print(myList[3][0]) ------> TypeError: 'bool' object is not subscriptable
``````
2 Likes

You just solved that for me in seconds. I broke my head around it for days.

Thanks a lot!!

1 Like

Hello folks happy new year. I have a question can some one please explain me the difference below:

Exercise Question: Use double square brackets (`[][]`) to select `Ellie`s test score from the list `class_name_test`. This time only use negative indices!

Save it to the variable `ellies_score`.

Print the variable `ellies_score` to see the result.

My code was :

class_name_test = [["Jenny" , 90] , ["Alexus" , 85.5] , ["Sam" , 83] , ["Ellie" , 101.5]] print (class_name_test) sams_score = class_name_test[2][1] print (sams_score) ellies_score = class_name_test [-1][1] print(ellies_score) class_name_test = [["Jenny" , 90] , ["Alexus" , 85.5] , ["Sam" , 83] , ["Ellie" , 101.5]] print (class_name_test) sams_score = class_name_test[2][1] print (sams_score) ellies_score = class_name_test [-1][1] print(ellies_score)

And I got the correct answer which is 101.5 but I received an error saying “Did you use negative indices?”

I actually used negative indices to reach ellies list and did this

ellies_score = class_name_test [-1][1]

it worked as per result but due to the warning I tried this:

ellies_score = class_name_test [-1][-1]

This worked as well and solved the error.

**So my question is what is the difference between **

ellies_score = class_name_test [-1][1]

and

ellies_score = class_name_test [-1][-1]
``````class_name_test = [["Jenny" , 90] , ["Alexus" , 85.5] , ["Sam" , 83] , ["Ellie" , 101.5]]
``````

In this particular exercise, `class_name_test[-1][1]` and `class_name_test[-1][-1]` both target the same element, so there is no difference between the two.
However, if the sub-list dimensions were different e.g.

``````class_name_test = [["Jenny" ,"abc", 90] , ["Alexus" , "def", 85.5] ,
["Sam" , "uvw", 83] , ["Ellie" , "xyz", 101.5]]
``````

then obviously we would have to use `class_name_test[-1][2]` and `class_name_test[-1][-1]` to target Ellie’s score.

For the exercise though, both `[-1][1]` and `[-1][-1]` target the same element. The error message is because both indices were expected to be negative. The intent probably being to make us more familiar with the use of negative indices.

`class_name_test[-1][1]` expects that the score would be the 2nd element of the last sub-list.

`class_name_test[-1][-1]` expects that the score would be the last element of the last sub-list.

If the structure of the sub-lists changes (e.g. some elements are inserted in the sub-lists) but the score remains the last element of the sub-list, then obviously `class_name_test[-1][-1]` will work fine even after the insertions, but `class_name_test[-1][1]` would have to be edited to account for the new dimensions.

1 Like

This was a godsend - a gazillion brain cells died trying to figure it out. Now it all adds up and made perfect sense.

1 Like

Thank you! Yeah the person who is responsible for writing this lesson should have explained it at least some.

Please explain why this numbers are in that order and how does it work in a case with more than two items per list. Thank you.
Here are the index numbers to access data for the list `heights`:
|Element|Index|
|`"Noelle"`|`heights[0][0]`|
|`61`|`heights[0][1]`|
|`"Ali"`|`heights[1][0]`|
|`70`|`heights[1][1]`|
|`"Sam"`|`heights[2][0]`|
|`67`|`heights[2][1]`|

@luisardi I think @mtrtmk’s explanation on the 2D list above with varying elements within a list provides an excellent explanation in what you are looking for:

For the example within the lesson, the example was:

``````heights = [["Noelle", 61], ["Ali", 70], ["Sam", 67]]
``````

heights is a list with 3 elements, each of which is a list.
So:

``````heights[0]  ------> ["Noelle", 61]
heights[1]  ------> ["Ali", 70]
heights[2]  ------> ["Sam", 67]
``````

Therefore:

``````heights[0][0]  ------> "Noelle"
heights[0][1]  ------> 61
heights[1][0]  ------> "Ali"
heights[1][1]  ------> 70
heights[2][0]  ------> "Sam"
heights[2][1]  ------> 67
``````

Thank you very much.
Cheers!

I saw the hint for the exercise, and I formatted my code to help visualize the list into a table like the following
class_name_test = [
[“Jenny”, 90],
[“Alexus”, 85.5],
[“Sam”, 83],
[“Ellie”, 101.5]
]
print(class_name_test)

However, when it gets printed, the output is in a single line. Is there a way to format the output to make it view like a table?

That statement only knows knows how to execute on the entire object. We need to break it down so it only sees a line a time. It will take a loop. Can you put this together?

1 Like

I haven’t learned loops yet, so I’ll have to get to that section and come back to this. Thanks for the hint.

2 Likes

This is my solution to the question. Thanks, @mtf

Define the Class Roster

class_name_test = [[“Jenny”, 90],[“Alexus”, 85.5],[“Sam”, 83],[“Ellie”, 101.5]]

Print the Class Roster as a Single Line

print(class_name_test)

Print the Class Roster separated with Line Breaks

for item in class_name_test:
print(item)

1 Like

This is another example of the directions being a bit unclear in lessons. They just want you to manually create a list of lists. `[["Jenny", 90], ["Alexus", 85.5]]` etc. Then use the indicies to access the sublists.
Ex:

``````[['Jenny, 90'], ['Alexus', 85.5], ['Sam', 83], ['Ellie', 101.5]]
83

sams_score = class_name_test[2][1]

print(sams_score)
>>83
``````

Got it…however when I did [2][1] for sam’s score it gave me the following index error:
IndexError: list index out of range

Any idea why?

Can you post ALL of your code? That will offer clues as to why you are getting the error.

To preserve code formatting in forums posts, see: How do I format code in my posts?

lass_name_test=[[‘Jenny’,90],[‘Alexus’,85.5],[‘Sam’,83],[‘Ellie’,101.5]]

print(class_name_test)

sams_score=[2][1]

print(sams_score)class_name_test=[[‘Jenny’,90],[‘Alexus’,85.5],[‘Sam’,83],[‘Ellie’,101.5]]

print(class_name_test)

sams_score=[2][1]

``````# You wrote:
class_name_test = [["Jenny",90],["Alexus",85.5],["Sam",83],["Ellie",101.5]]

sams_score = [2][1]  # <--- Incorrect --- IndexError

print(sams_score)
``````

You want to access nested elements from the list assigned to the `class_name_test` variable, so the correct syntax should be:

``````sams_score = class_name_test[2][1]

print(sams_score) # 83
``````

Since you omitted the name of the list from the statement,

``````# You wrote:
sams_score = [2][1]
``````

the first bracket notation is being interpreted as a list with a single element `[2]` i.e. a list whose only element is the integer `2`.
The second bracket is being interpreted as an attempt to use an index to access an element from this `[2]` list. Index `1` means you are trying to access the second element (since Python lists are zero-indexed) of the `[2]` list, but there is no element at this index. Hence the `IndexError`.

Consider the example,

``````x = [8, 16, 25, 33][1]

print(x) # 16
# In this case, there is an element at index 1, so no error is thrown.
``````
1 Like

Great…thanks so much

1 Like