I have a list and I want to remove the dublicates(tiplicates) from this list.
My approach was:
-
sort the list
-
check if two adiacent items are the same, if so eliminate one of them.
inventory = ["twin bed", "twin bed", "headboard", "queen bed", "king bed", "dresser", "dresser", "table", "table", "nightstand", "nightstand", "king bed", "king bed", "twin bed", "twin bed", "sheets", "sheets", "pillow", "pillow_last"]
i = 0
for items in inventory:
if inventory[i] == inventory[i+1]:
inventory.pop(i)
print(inventory,i)
i -=1
i +=1
print(inventory)
Looks like it will work but it will stop when i=9.
Can you please hel me understand why?
L.E:
Looks like
inventory.sort()
i = 0
for a in range(len(inventory)-1):
if inventory[i] == inventory[i+1]:
inventory.pop(i)
print(inventory,i)
i -=1
i +=1
print(inventory)
print(len(inventory))
works better.