Removing duplicate strings from a list in python [duplicate]

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

Leave a Comment

tech