Remove Duplicate Elements From a List in Python Using OrderedDict.fromkeys()

Python Program to Remove Duplicate Elements From a List Using collections.OrderedDict.fromkeys()

# Remove Duplicate Element From a List Using collections.OrderedDict.fromkeys() in python
 
from collections import OrderedDict

# initializing list
test_list = [1, 5, 3, 6, 3, 5, 6, 7,1]
print ("The original list is : "
	+ str(test_list))

# using collections.OrderedDict.fromkeys() to remove duplicated from list
res = list(OrderedDict.fromkeys(test_list))

# printing list after removal
print ("The list after removing duplicates : "
	+ str(res))

Output:

The original list is : [1, 5, 3, 6, 3, 5, 6, 7, 1]
The list after removing duplicates : [1, 5, 3, 6, 7]

About The Author

Leave a Reply