0

Hey everyone i have this issue here, i really can't make out what the problem is. I know that range(2) gives 0 and 1 for possible i. len(R) = 2. And lists starts with 0.

R = [[1,1],[5,5]]
for i in range(len(R)):
    if R[i][0] == R[i][1]:     
        R.remove(R[i])

the error:

if R[i][0] == R[i][1]:

IndexError: list index out of range

2 Answers2

2
R = [[1,1],[5,5]]
R = [x for x in R if x[0] != x[1]]

It should be != instead of == since he wants to get the numbers that are not identical.

Lukas Herman
  • 170
  • 2
  • 9
Błotosmętek
  • 12,717
  • 19
  • 29
-3

In general, It's much easier to iterate over the items in the list:

Removal_items =[]
for I in R:
  #I is now a variable that represents the object in the list, or I = R[0]...R[n] where n = len(r) -1
  if I[0] == I[1]:
  Removal_items.append(I)

for I in Removal_items:
  R.remove(I)
Owen Hempel
  • 434
  • 2
  • 8