+ 1
why I get type error here, it prints element number 80 alone but I get type error when I try to print two elements
numbers= list(range(1200,2000)) print(numbers[80][81])
2 Respostas
+ 5
numbers[80][81]
This syntax assumes that you have a 2-dimensional list, meaning that each element of your list is actually another list that contains the final elements.
You want to print two elements, index them separately.
print(numbers[80], numbers[81])
or you can use a slice, if the elements are next to each other
print(numbers[80:82])
+ 2
The “typeerror: 'int' object is not subscriptable” error is raised when you try to access an integer as if it were a subscriptable object, like a list or a dictionary. To solve this problem, make sure that you do not use slicing or indexing to access values in an integer.
- careerkarma.com
What does it mean?
The error is indicating that the function or method is not subscriptable; means they are not indexable like a list or sequence.
- stackoverflow
An example
>>> li = [ 1 , 2 , 3 ]
>>> li[0]
1
>>> a = li[0]
>>> a
1
>>> a[5]
TypeError: 'int' object is not subscriptable
>>> li[0][5]
TypeError: 'int' object is not subscriptable
>>> li = [ [ 1,2] , [3,4] ]
>>> li[0]
[ 1 , 2 ]
>>> li[0][1]
2
In your case you doing same , in 80th index of your list number is 1280 and then you trying to indexing int which is an error!