How To Remove Duplicates From Python List Preserving Order

In this article, we explain how to remove duplicate items from a Python list while preserving initial order. This is also known as Uniquifying a List

For example, if you have a list like [1, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7] and you want to remove duplicates and preserve order i.e. 1, 2, 3, 4, 5, 6, 7.

Python Source Code: Uniquifying List


# input
original_list = [1, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7]

# uniquifying
unique_list = list(dict.fromkeys(original_list))

# output
print('Unique list: ', unique_list)

Output

Unique list:  [1, 2, 3, 4, 5, 6, 7]

Breaking it Down

To understand more clearly see following breakdown and analyze intermediate result.


original_list = [1, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 7]
temp = dict.fromkeys(original_list)
print('Temp: ', temp)
unique_list = list(temp)
print('Unique list: ', unique_list)

Output

Temp:  {1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None}
Unique list:  [1, 2, 3, 4, 5, 6, 7]