My code can remove duplicate items but destroys the order of the list
ls = eval(input("Input a list:"))
lss = list(set(ls))
print(lss)
I searched and tried some codes but it isn't working for me.
My code can remove duplicate items but destroys the order of the list
ls = eval(input("Input a list:"))
lss = list(set(ls))
print(lss)
I searched and tried some codes but it isn't working for me.
The order is not preserved because you convert your list to set, which is unordered, unique collection.
Try this one:
from collections import OrderedDict
items = eval(input("Input a list:"))
print(list(OrderedDict.fromkeys(items)))
# for example:
items = [1, 2, 0, 1, 3, 2]
print(list(OrderedDict.fromkeys(items)))
# output: [1, 2, 0, 3]
lst = input("Input List: ")
lst2 = []
for i in lst:
if i not in lst2:
lst2.append(i)
print(lst2)
Apologies for my previous failed attempt. Hopefully this one works