How to remove duplicates from a Python list or array?
Published on Aug. 22, 2023, 12:14 p.m.
To remove duplicates from a Python list or array, you can use the set() function on the list. Here is an example:
my_list = [1, 2, 3, 2, 4, 3]
unique_list = list(set(my_list))
print(unique_list)
This code will output [1, 2, 3, 4], which is the original list with duplicates removed.
If you want to preserve the original order of the list, you can use a combination of set() and a list comprehension:
my_list = [1, 2, 3, 2, 4, 3]
unique_list = []
for item in my_list:
if item not in unique_list:
unique_list.append(item)
print(unique_list)
This code will also output [1, 2, 3, 4], but the order of the items will be preserved.
If you’re working with a NumPy array, you can use the np.unique() method:
import numpy as np
my_array = np.array([1, 2, 3, 2, 4, 3])
unique_array = np.unique(my_array)
print(unique_array)
This code will also output [1, 2, 3, 4].