xxxxxxxxxx
# Approach 1: Using set
original_list = [1, 2, 3, 2, 4, 5, 1, 3, 3, 4, 5]
unique_list = list(set(original_list))
print(unique_list)
# Approach 2: Using a loop
original_list = [1, 2, 3, 2, 4, 5, 1, 3, 3, 4, 5]
unique_list = []
for element in original_list:
if element not in unique_list:
unique_list.append(element)
print(unique_list)
xxxxxxxxxx
import numpy as np
def unique(list1):
npArray1 = np.array(list1)
uniqueNpArray1 = np.unique(npArray1)
return uniqueNpArray.tolist()
list1 = [10, 20, 10, 30, 40, 40]
unique(list1) # [10, 20, 30, 40]
xxxxxxxxxx
>>> items = [1, 2, 0, 1, 3, 2]
>>> list(dict.fromkeys(items)) # Or [*dict.fromkeys(items)] if you prefer
[1, 2, 0, 3]
xxxxxxxxxx
# just turn it into a set and then convert again into a list
res = list(set(lst1)))
# now check the lengths of the two lists
print(len(res))
print(len(lst1))
xxxxxxxxxx
# Method 1 = convert to set to remove duplicate and then convert back to list
mylist = list(set(mylist))
# Method 2 = use numpy to convert list to array and the apply unique method
np.unique(np.array(mylist)) # You can change the array back to list
# Method 3 = For pandas dataframe
df["col"].unique()
xxxxxxxxxx
mylist = ['nowplaying', 'PBS', 'PBS', 'nowplaying', 'job', 'debate', 'thenandnow']
myset = set(mylist)
print(myset)