Press "Enter" to skip to content

How to Remove Duplicate Items from a List in Python?

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:

Have you found the answer to your question? If not, you can discuss it with me in the comments section below or join my Q&A community OrclQA.com for developers and ask your question. It is FREE.

Vinish Kapoor

Follow

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

guest
0 Comments
Inline Feedbacks
View all comments