-1

I've got this simplified list of lists:

nlist = [['E:\\photos\\IMG 1860 - Copy.jpg', 'E:\\photos\\IMG 1860.jpg', 'E:\\photos\\IMG 1873(2).jpg', 'E:\\photos\\IMG 1873(2).jpg']]

What I want to achieve is to remove duplicates from it, my code does not work and I know why but I cannot figure out workaround:

for b in range(len(nlist[0])-1):
    if nlist[0][b] == nlist[0][b+1]:
        print(nlist[0][b]) #it prints this line but get "IndexError: list index out of range" after
        del nlist[0][b]

Any suggestions will be appreciated.

imperato
  • 329
  • 2
  • 12
  • 1
    You're modifying the list while you're iterating over it - that's a recipe for disaster (the range will iterate over the original length, but you're making it smaller inside your loop). Instead, you can create a set - it'll only contain unique values. so `set(nlist)` and avoid the whole loop construct. – MatsLindh Aug 25 '20 at 09:32

2 Answers2

1

Like this

nlist = [['E:\\photos\\IMG 1860 - Copy.jpg', 'E:\\photos\\IMG 1860.jpg', 'E:\\photos\\IMG 1873(2).jpg', 'E:\\photos\\IMG 1873(2).jpg']]

no_duplicates = [list(set(lst)) for lst in nlist]
print(no_duplicates)

output

[['E:\\photos\\IMG 1873(2).jpg', 'E:\\photos\\IMG 1860.jpg', 'E:\\photos\\IMG 1860 - Copy.jpg']]
balderman
  • 22,927
  • 7
  • 34
  • 52
0

You could simply use a set:

list(set(nlist[0]))

This removes only the elements with the same name, will not remove the - Copy names.

pbuzulan
  • 67
  • 6