Error: list index out of range | How to declare a list?
Question:
I am facing a problem in python. Here is my code:
x = int(input())
a = list()
for c in range(1,x+1):
for i in range(0,3):
for j in range(0,3):
if j == 2 and i == 1:
a[c][i][j] = a[c][1][1]
a[c][1][1] = None
continue
a[c][i][j] = int(input())
print(a)
The error message looks like this:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-12-96a108d4a9f6> in <module>
8 a[c][1][1] = None
9 continue
---> 10 a[c][i][j] = int(input())
11 print(a)
IndexError: list index out of range
Now my question is why it is happening? I have declared the list. Oh, another thing. As you see I am taking the values from the loop. So I can’t declare any list like
a = [[2,3],[5,6]]
So how can I solve this problem? or can you suggest another way to get input in a list?
I am using append. But it creates another problem. How to inter first value like a[0][0][0]?
Answers:
The error means that you are accessing an index which is out of range of your list.
When you declare your list ‘a’, it’s still empty, so you can’t modify the values inside because there’s no values inside. You can’t access for example a[5] if a doesn’t have 6 items (because index starts by 0) in it.
One option is to fill your list first and then modified the values or you can append the value as you loop through.
I get it. Now I am declaring a = [[[n]]]
first. Here n is any natural number. Then append the values with this a[i][j].append(x)
Like this
a = [[[2]]]
a[0][0].append(1)
print(a)
It will return this
[[[2,1]]]
I am facing a problem in python. Here is my code:
x = int(input())
a = list()
for c in range(1,x+1):
for i in range(0,3):
for j in range(0,3):
if j == 2 and i == 1:
a[c][i][j] = a[c][1][1]
a[c][1][1] = None
continue
a[c][i][j] = int(input())
print(a)
The error message looks like this:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-12-96a108d4a9f6> in <module>
8 a[c][1][1] = None
9 continue
---> 10 a[c][i][j] = int(input())
11 print(a)
IndexError: list index out of range
Now my question is why it is happening? I have declared the list. Oh, another thing. As you see I am taking the values from the loop. So I can’t declare any list like
a = [[2,3],[5,6]]
So how can I solve this problem? or can you suggest another way to get input in a list?
I am using append. But it creates another problem. How to inter first value like a[0][0][0]?
The error means that you are accessing an index which is out of range of your list.
When you declare your list ‘a’, it’s still empty, so you can’t modify the values inside because there’s no values inside. You can’t access for example a[5] if a doesn’t have 6 items (because index starts by 0) in it.
One option is to fill your list first and then modified the values or you can append the value as you loop through.
I get it. Now I am declaring a = [[[n]]]
first. Here n is any natural number. Then append the values with this a[i][j].append(x)
Like this
a = [[[2]]]
a[0][0].append(1)
print(a)
It will return this
[[[2,1]]]