Remove Duplicate Element From a List Using list comprehension in Python

Python Program to Remove Duplicate Elements From a List Using list comprehension

# Remove Duplicate Element From a List Using list comprehension in Python
 
# initializing list
test_list = [1, 3, 5, 6, 3, 5, 6, 1,2,6,4]
print("The original list is : "
	+ str(test_list))

# Using list comprehension to remove duplicates from list
res = []
[res.append(x) for x in test_list if x not in res]

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

Output:

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

About The Author

Leave a Reply