Remove duplicates from list in Python

The easiest python way to remove duplicates from the list (in other words deduplicate) is to convert the list to set and then convert it back to the list.

my_list = [1, 2, 4, 6, 6, 3, 6, 5, 5, 9, 2]
print(f"List with duplicates: {my_list}")

dedup_list = list(set(my_list))
print(f"List without duplicates: {dedup_list}")

Output:

List with duplicates: [1, 2, 4, 6, 6, 3, 6, 5, 5, 9, 2]
List without duplicates: [1, 2, 3, 4, 5, 6, 9]

The drawback for some use cases ⁠– this way doesn't preserve element order. If you need to keep order, then see below.

Remove duplicates from list python with preserving order

dedup_list = []
for i in my_list:
  if i not in dedup_list:
    dedup_list.append(i)

Deduplicate python list

#python
menu 11
Roman Ivashchenko profile picture
Nov 10, 2020
by Roman Ivashchenko
Did it help you?
Yes !
No

Best related

Other by Roman Ivashchenko