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.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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?
>>> 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.
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.
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?
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.
@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.
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.