-2
index=[[25, 0], [25, 1], [25, 0], [25, 4], [25, 1], [25, 4], [56, 2], [56, 3]]
for x in range(0,len(index)):
    for y in range(x+1,len(index)):
        if index[x][-1]==index[y][-1]:
            index.remove(index[y])
print(index)

Got the following error:

 if index[x][-1]==index[y][-1]: IndexError: list index out of range
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
vijay e
  • 9
  • 1
  • 1
    Explain what are you trying to do – Manuel Fedele Dec 30 '20 at 08:13
  • it would be easier if you convert these nested `lists` into a `tuple` and then use `set`. – Vishal Singh Dec 30 '20 at 08:18
  • Now is a good time to learn the practices of basic debugging. Your post should include your trace of `x, y,` and `index` on each loop iteration, especially just before the point of failure. You also should not ask how to remove duplicates, as that is a straightforward lookup. Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). You haven't yet done the expected research. – Prune Dec 30 '20 at 08:24
  • `newlist = [x for i,x in enumerate(mylist) if x not in mylist[:i]]` will do the trick – Joe Ferndz Dec 30 '20 at 08:25

3 Answers3

1

You are looping over a list and deleting the same list simultaneously .. It ran out of index with which the loop was started. Make a copy of list and then run over it.

Slightly edited code.

index=[[25, 0], [25, 1], [25, 0], [25, 4], [25, 1], [25, 4], [56, 2], [56, 3]]
index_for_loop = index.copy()
for x in range(0,len(index_for_loop)):
    for y in range(x+1,len(index_for_loop)):
        if index_for_loop[x]==index_for_loop[y]:
            index.remove(index_for_loop[y])
print(index)

Gives output

[[25, 0], [25, 1], [25, 4], [56, 2], [56, 3]]
Amit
  • 2,018
  • 1
  • 8
  • 12
1

One-liner:

index = list(map(list, (set(tuple(a) for a in index))))

gives

[[25, 4], [25, 0], [56, 3], [56, 2], [25, 1]]

Lists are unhashable in Python, hence you need to convert them to tuple first.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
0

You can try the following

index = [[25, 0], [25, 1], [25, 0], [25, 4], [25, 1], [25, 4], [56, 2], [56, 3]]

lst = []

for i in range(len(index)):
    if index[i] not in lst:
        lst += [index[i]]

print(lst)
>>> [[25, 0], [25, 1], [25, 4], [56, 2], [56, 3]]
Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52