How to remove duplicate elements from an array in Python ? | Python coding for beginners

Publié le: 18 avril 2020
sur la chaîne: Engineers Revolution
1,586
14

Removing Duplicates from a List
Python list can contain duplicate elements. Let’s look into examples of removing the duplicate elements in different ways.

1. Using Temporary List
This is the brute-force way to remove duplicate elements from a list. We will create a temporary list and append elements to it only if it’s not present.


ints_list = [1, 2, 3, 4, 3, 2]

temp = []

for x in ints_list:
if x not in temp:
temp.append(x)

ints_list = temp

print(f'Updated List after removing duplicates = {temp}')
Output: Updated List after removing duplicates = [1, 2, 3, 4]
2. set() function
Python set doesn’t have duplicate elements. We can use the built-in set() function to convert the list to a set, then use the list() function to convert it back to the list.


ints_list = [1, 2, 3, 4, 3, 2]

ints_list1 = list(set(ints_list))
print(ints_list1) # [1, 2, 3, 4]
3. List elements as Dictionary Keys
We know that dictionary keys are unique. The dict class has fromkeys() function that accepts an iterable to create the dictionary with keys from the iterable.


ints_list = [1, 2, 3, 4, 3, 2]

ints_list2 = list(dict.fromkeys(ints_list))
print(ints_list2) # [1, 2, 3, 4]
4. List count() function
The list count() method returns the number of occurrences of the value. We can use it with the remove() method to eliminate the duplicate elements from the list.

5. List Comprehension
We can create a list from an iterable using the list comprehension. This technique is the same as using the temporary list and the for loop to remove the duplicate elements. But, it reduces the number of lines of the code.


int_list = [1, 2, 3, 4, 3, 2]
temp = []
[temp.append(x) for x in ints_list if x not in temp]
print(temp) # [1, 2, 3, 4]
Best Way to Remove Duplicates from a List
If you don’t want duplicate elements, you should use Set. But, if you have to remove the duplicate values from a list, then I would prefer count() function because it doesn’t create another temporary set or list object. So, it’s more memory efficient.


Sur cette page du site, vous pouvez voir la vidéo en ligne How to remove duplicate elements from an array in Python ? | Python coding for beginners durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Engineers Revolution 18 avril 2020, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 1,586 fois et il a aimé 14 téléspectateurs. Bon visionnage!