0

I am trying to define a two dimension array with for loops using range function:

>>> takobaba =  [['A' for i in range(0, 6, 1)] for j in range(0, 6, 1)]
>>> takobaba [-1][-1]
'A'

As you can see i get an output for index -1, -1 even i called range function from 0 to 6 with step 1 for both dimensions. Could you please help me to understand the reason and fix this error?

takobaba
  • 306
  • 1
  • 4
  • 15
  • 1
    indexing with negative value means reverse indexing, from last element up to first element...so `[-1][-1]` means returning last element of last row – Iron Fist Dec 28 '15 at 21:55
  • is there a way to stop this reverse indexing for my work – takobaba Dec 28 '15 at 21:57
  • 1
    @takobaba you could write your own list class. But... why? – timgeb Dec 28 '15 at 21:58
  • 1
    You mean, you don't want to get elements with negative values? – Iron Fist Dec 28 '15 at 22:01
  • I am doing some addition and removal on indexes so this kind of bothers me but i figured out a way if thats how python works. I am going to prevent negative indexes. Thank you all. – takobaba Dec 28 '15 at 22:03

0 Answers0