Remove Duplicate Elements From a List in Python Using Array.index()

Python Program to Remove Duplicate Elements From a List Using Array.index()

# Python Program to Remove Duplicate Elements From a List Using Array.index()

# initializing list
arr = [1 ,4 , 5, 6, 9, 6, 3, 8, 5, 6, 1]
print ('The original list is : '+ str(arr))
 
# using list comprehension + arr.index() to remove duplicated from list
res = [arr[i] for i in range(len(arr)) if i == arr.index(arr[i]) ]
 
# printing list after removal of duplicate
print('The list after removing duplicates :' ,res)

Output:

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

About The Author

Leave a Reply