Reputation: 818
I want to go through a 2D array in reverse. Thats is why I use reversed() but I get an error that says
list indices must be integers, not list
Array example:
labirynth = [
[1,1,1,1,1,1,1],
[1,0,0,0,1,0,1],
[1,0,1,0,0,0,1],
[1,1,1,1,1,1,1]
]
My current solution:
for i in reversed(labirynth):
for j in reversed(labirynth[i]):
#do stuff
Upvotes: 0
Views: 2982
Reputation: 14645
You probably want
for i in reversed(labirynth):
for j in reversed(i):
# do stuff
here's an interactive demo:
>>> labirynth = [
[1,1,1,1,1,1,1],
[1,0,0,0,1,0,1],
[1,0,1,0,0,0,1],
[1,1,1,1,1,1,1]
]
... ... ... ... ...
>>> for i in reversed(labirynth):
... print i
...
[1, 1, 1, 1, 1, 1, 1]
[1, 0, 1, 0, 0, 0, 1]
[1, 0, 0, 0, 1, 0, 1]
[1, 1, 1, 1, 1, 1, 1]
>>> for i in reversed(labirynth):
... for j in reversed(i):
... print j
...
1
1
1
... continues
Upvotes: 0
Reputation: 1715
There is no need to access the list with []
. The outer for loop already returns the list. You can just do
for i in reversed(labirynth):
for j in reversed(i):
# do stuf...
Upvotes: 1