1)To get rid of duplicate items, convert the list to a set.
2)Compare the original list's length to the set's length. There are no duplicates in the list if they are the same. Otherwise, there are duplicates on the list.
For ex:
my_list = [1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9]
# check if the list contains duplicates
if len(my_list) != len(set(my_list)):
# if the list contains duplicates, create a new list without duplicates
new_list = []
for item in my_list:
if item not in new_list:
new_list.append(item)
else:
# if the list does not contain duplicates, use the original list
new_list = my_list
print(new_list) # output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Master the art of Data Science with Python and unlock the power of insights.