1

I'm trying to compare every first elements inside a python nested list so I decided to loop in the length of list-1 to compare the element with next, but I'm still getting "list index out of range". Isn't it supposed to compare the first with second, the second with third, and third with fourth, for a total of 3 interactions?

A =[[1,2,3]
   ,[2,5,6]
   ,[5,2,3]
   ,[5,2,3]
   ]

S = len(A) - 1
for n in range(S):
    if(A[0][n]) == (A[0][n+1]):

        print("no")
    else:
        print('yes we did it')
ophunt
  • 100
  • 1
  • 10
Yefet
  • 2,010
  • 1
  • 10
  • 19

5 Answers5

1

you have to swap indexes:

A =[[1,2,3]
   ,[2,5,6]
   ,[5,2,3]
   ,[5,2,3]
   ]
S = len(A) - 1
for n in range(S):
    if(A[n][0]) == (A[n+1][0]):
        print("no")
    else:
        print('yes we did it')
1

So as @ophunt said. You are currently trying to loop the elements in the first and only first list. But you said you wanted to compare the first element in each list.

In your if statement you write:

   if(A[0][n]) == (A[0][n+1]):

This is what is making the loop throw an index out of range error. The first index should be your counter and the second index is 0 since you want the first element of that list.

    if(A[n][0]) == (A[n+1][0]):

This leaves you with.


    A =[[1,2,3]
   ,[2,5,6]
   ,[5,2,3]
   ,[5,2,3]
   ]

    S = len(A) - 1
    for n in range(S):
        if(A[n][0]) == (A[n+1][0]):

            print("no")
        else:
        print('yes we did it')

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
0

Two errors I can see:

  1. S stores the length of A, which is 4, as A contains 4 sub-lists. However, you're looping through the sub-lists, which have length 3, not A.

  2. When you check A[0][n+1], this will check an out of bounds index as n goes up to the final element in A, and so n+1 will be out of bounds.

ophunt
  • 100
  • 1
  • 10
  • You're very close to correct, look at Rashid's answer in regards to iterating over the correct indices. The first element of every list is at [0], you just to need to iterate over the lists themselves. Keep in mind that my second point is still valid, as a list with n elements can only be accessed at indices up to n-1, and your code will attempt to access n-1+1, throwing an out of bounds error. – ophunt Aug 05 '19 at 19:14
0

Transpose the list so you can iterate over columns (zip(*A); iterate over pairs in the column (zip(thing,thing[1:]).

for thing in zip(*A):
    for x,y in zip(thing,thing[1:]):
        print(x,y,x==y)

1 2 False
2 5 False
5 5 True
2 5 False
5 2 False
2 2 True
3 6 False
6 3 False
3 3 True

Transpose list of lists
Iterate over all pairs of consecutive items in a list

wwii
  • 23,232
  • 7
  • 37
  • 77
0

len(A) is 4, not 3, because it is counting the number of lists in A, not the length of each list. Thus, eventually, your if statement will ask for A[0][3], which is undefined.

A =[[1,2,3]
   ,[2,5,6]
   ,[5,2,3]
   ,[5,2,3]
   ]

S = len(A[0]) - 1
for n in range(S):
    if(A[0][n]) == (A[0][n+1]):

        print("no")
    else:
        print('yes we did it')

len(A[0]) will be 3, not 4, so it should work.