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

This community-built FAQ covers the “Accessing 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 Accessing 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!

I have previously read that if the list is arrays, print(list[index]) is going to lead to an error if the value corresponding to that index is an float

Yet for a 2D list, a float value will not lead to an error in this case…

Why is this occurring?

Update:

It seems that this error only applies when one divides the index by something, I have tried the following code to elucidate myself:

test1 = [1.1, 2.2]

print(test1[0])

This gives 1.1 as the answer.

print(test1[3/3])

#This line has the mistake, no int()

print(test1[int(3/3)])

#above is the fixed version of the line with a mistake

This line prints 2.2, which corresponds to index 1

Thus, my conclusion could be summarize to “if you want to do a division of the index, its attached value must be an integer - if it is a float, it will lead to an error”. Am I correct?

The very nature of enumeration excludes floats. An index can only ever use counting numbers.

Values contained in the list can be of any type and have no bearing on their index, which is position based.

Oh I see, so index can reference float or integers. However, if the value of the index itself isn’t an integer, an error will appear. Is that correct?

Yes, Python will raise an error.

``````>>> arr = [3, 6, 7, 8, 0]
>>> arr[1.0]
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
arr[1.0]
TypeError: list indices must be integers or slices, not float
>>>
``````

When we reference an element in a list we are said to be subscripting its position within the serial index associated with the list. That index is virtual, since it doesn’t physically exist. Accessing lists is done by counting from the first element up to the one we wish to poll or mutate. We don’t have to worry about that depth of involvement, thank heaven. Subscripts are our saviors.

``````print (arr[1])     #  6

print (arr[4])     #  0

print (arr[1:])    #  [6, 7, 8, 0]   =>  slice
``````

If we want for our purposes to have a physical index of a list, then we must enumerate it. This will give us a list of index/value pairs (also a 2D list).

To manually enumerate a list we have only to iterate it and spin off a new index/value pair list.

``````>>> enum_arr = []
>>> for i in range(len(arr)):
index = i
value = arr[index]
enum_arr.append([index, value])

>>> print (enum_arr)
[[0, 3], [1, 6], [2, 7], [3, 8], [4, 0]]
>>>
``````

Above we are not actually iterating the list, but a range that equates to the virtual index sequence we use to access the reference list.

When accessing a 2D order list using negative indices do both have to be negative. Accessing ellies_score = class_name_test[-1][-1] is the same as ellies_score = class_name_test[-1][1]. Is this just the common format of writing, or does this have greater impact if there are lists of higher order?

Simple answer, no. The two indices are independent just as their lists are independent. If the outer list is the rows, and the inner list is the columns, the first index accesses the rows, and the second the columns.

1 Like

ok thanks so it was just a preference for the exercise then

1 Like

I’m not understanding the part where it talks about how to access Noelle’s height. I’m not getting where y’all got the numbers [0] and [1] from.

1 Like

Given a list,

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

we access the various data points of the list by using a subscript and index, `list[index]`.

``````print (a[5])    #  6
``````

The index starts at zero (0) so index[5] is actually the sixth element in the list.

What if we turn all those numbers into lists?

``````b = [
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10],
[11, 12],
[13, 14],
[15, 16],
[17, 18]
]
``````

Now we print the sixth element we get,

``````print (b[5])
``````

we get,

``````[11, 12]
``````

To access the second element of that list we write a subscript for the row ([5]) and a subscript for the column ([1]), so,

``````print (b[5][1])    #  12
``````

Starting to make sense?

2 Likes

I’m still confused with Ellie’s scores. It says to use negative indices only, but I don’t understand how that corresponds. The positive indices make sense as they are rows and columns but how do you translate that to the negative indices?

If we look at a number line associated with a list, say,

``````u = ['a', 'b', 'c', 'd', 'e', 'f']
``````

it will look like this,

``````-6 -5 -4 -3 -2 -1  0  1  2  3  4  5
``````

and when we break it in half,

``````  -6   -5   -4   -3   -2   -1     #  right to left
['a', 'b', 'c', 'd', 'e', 'f']
0    1    2    3    4    5     #  left to right
``````

A general expression to describe the index range of a list where n is the length, and x is an index in range,

``````{x | -n <= x < n; x, n are INT}
``````

The corresponding rtl index is the ltr index minus the length. The corresponding ltr index is rtl index plus the length.

3 Likes

Great, thank you for this answer, came here with the same question.

1 Like

I am extremely confused by the second step of the exercise. More specifically, I am confused by accessing the data from Sam’s score.

How is the answer [4][5] as opposed to [2][1]? I mean, if we are accessing the data via index numbers, wouldn’t Sam’s data correspond to the points [4][5] and not [2][1]?

In part, my confusion stems from the last section in the tutorial and the lack of explanation of the index number and elements.

Thanks!

@text2048143998 I was confused myself at first, especially by the [1] part. If you copy and paste the code into a .py file and make the changes you’re suggesting it might help you understand.

Try copying and running this:
class_name_test = [[“Jenny”, 90], [“Alexus”, 85.5], [“Sam”, 83], [“Ellie”, 101.5]]
print(class_name_test)
sams_score = class_name_test[2][0]
print(sams_score)

Sam is the 3rd entry on the first column of the list so that’s where you get the [2] from
Sam’s score is the 2nd entry on that row so that’s why it’s [1]

I only realised this when playing around with the numbers in the above code.

1 Like

im stuck at the last one can you solve this problem?:

ellies_score = [
[class_name_test, ellies_score[-2]-[1]]
]

Ellie’s stuff is last in the array, so you’d use index `-1`,
and the score is the last thing in `["Ellie", 101.5]` so you’d use `-1` again

``````ellies_score = class_name_test[-1][-1]
``````

i already did this: ellies_score = class_name_test[-1][-1] but i get this:

error:File “script.py”, line 6
print(class_name_test)
^
SyntaxError: invalid syntax

It worked when I did it.

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