How to Remove Duplicates from a list in Python

There are several ways to remove duplicates from a list in Python. Here are three common methods:

  1. Using the set() function: The set() function can be used to remove duplicates from a list by converting the list to a set and then back to a list.
my_list = [1, 2, 3, 3, 4, 4, 5]
new_list = list(set(my_list))
print(new_list)

Output:

[1, 2, 3, 4, 5]
  1. Using a for loop and a new list: This method involves iterating over the original list and appending each element to a new list if it hasn’t already been added.
my_list = [1, 2, 3, 3, 4, 4, 5]
new_list = []
for i in my_list:
    if i not in new_list:
        new_list.append(i)
print(new_list)

Output:

[1, 2, 3, 4, 5]
  1. Using list comprehension: List comprehension can also be used to remove duplicates from a list.
my_list = [1, 2, 3, 3, 4, 4, 5]
new_list = list(dict.fromkeys(my_list))
print(new_list)

Output:

[1, 2, 3, 4, 5]

Conclusion:

In Python, there are several ways to remove duplicates from a list. You can use the set() function to convert the list to a set and then back to a list, or use a for loop to iterate over the original list and append each element to a new list if it hasn’t already been added. List comprehension is also another option, using the dict.fromkeys() method to convert the list to a dictionary with the list elements as keys and then back to a list. Each of these methods produces a new list with the duplicates removed.