Remove Duplicate Elements From a List in Python Using enumerate()

Python Program to Remove Duplicate Elements From a List Using enumerate()

# Python Program to Remove Duplicate Elements From a List Using enumerate()
 
# initializing list
test_list = [1, 7, 5, 3, 6, 3, 5, 6, 1]
print ("The original list is : "
		+ str(test_list))

# using list comprehension + enumerate() to remove duplicates from list
res = [i for n, i in enumerate(test_list) if i not in test_list[:n]]

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

Output:

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

About The Author

Leave a Reply