Python remove duplicates from list

The easiest way to remove duplicates from the list (in other words deduplicate) is to convert the list to set and then convert 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]

Drawback for some use cases ⁠– this way doesn't preserve element order. If you need keep an 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)

#python
menu 11
Roman Ivashchenko profile picture
Nov 10, 2020
by Roman Ivashchenko

Best related

Other by Roman Ivashchenko