/  Technology   /  Removing duplicates in lists in python Part-1
Removing duplicates in lists Part-1

Removing duplicates in lists in python Part-1

In this article, let’s discuss the different ways in which we can make a unique list by removing all the duplicates present in it. 

  • Using a loop with append()
  • Using List Comprehension
  • Using List Comprehension with enumerate()

 

Using a loop with append():

In this method, we simply traverse the given list named original_list and append the first occurrence of the element to a new list named unique_list and ignore all the rest of the occurrences of that particular element. This ensures the duplicates aren’t repeated.

Example:

original_list = [1,3,5,7,1,3,5,9,11]
print ("original list is : " + str(original_list))

unique_list = []
for i in original_list:
    if i not in unique_list:
        unique_list.append(i)

print("List after removing duplicates : " + str(unique_list))

output:

 

Using List Comprehension:

This method works much similar to the above method but it’s a one-liner code and a shorter version of the above. We use the keywords ‘in’ and ‘not’ in keywords to know the existence of an element in the original list and the unique list to ensure no duplicates are present.\

Example:

list = [1,3,5,7,1,3,5,9,11]
print ("original list is : " + str(list))

unique_list = []
[unique_list.append(x) for x in list if x not in unique_list]

print("List after removing duplicates : " + str(unique_list))

output:

 

 

Using List Comprehension with enumerate():

This is one of the most popular ways to remove duplicates from a list. But one of the drawbacks is that the ordering of the elements is lost in the newly created list. The enumerate() function keeps count of the number of iterations by adding a counter to an iterable and returns this iterable as an enumerating object. 

Example:

list = [1,3,5,7,1,3,5,9,11]
print ("original list is : " + str(list))

unique_list = [i for n, i in enumerate(list) if i not in list[:n]]

print("List after removing duplicates : " + str(unique_list))

output:

 

Leave a comment