If I have a string list ,
a = ["asd","def","ase","dfg","asd","def","dfg"]
how can I remove the duplicates from the list?
If I have a string list ,
a = ["asd","def","ase","dfg","asd","def","dfg"]
how can I remove the duplicates from the list?
Convert to a set:
a = set(a)
Or optionally back to a list:
a = list(set(a))
Note that this doesn't preserve order. If you want to preserve order:
seen = set()
result = []
for item in a:
if item not in seen:
seen.add(item)
result.append(item)
See it working online: ideone
def getUniqueItems(iterable):
result = []
for item in iterable:
if item not in result:
result.append(item)
return result
print (''.join(getUniqueItems(list('apple'))))
P.S. Same thing like one of the answers here but a little change, set is not really required !
You could pop 'em into a set
and then back into a list:
a = [ ... ]
s = set(a)
a2 = list(s)