How to Remove Duplicate Items from a List in Python?

  • Python
  • 1 min read

Here is a Python program to remove duplicate items from a list and will preserve the order.

Remove Duplicate Items From a List in Python

def dedupe(items):
    dup_item = set()
    for item in items:
        if item not in dup_item:
            yield item
            dup_item.add(item)

if __name__ == '__main__':
    a = [4, 3, 2, 4, 9, 4, 3, 8]
    print(a)
    print(list(dedupe(a)))

Output:

[4, 3, 2, 4, 9, 4, 3, 8]
[4, 3, 2, 9, 8]

Process finished with exit code 0

See also: