How can I remove duplicates from a list in Python?
Alex K
alex k profile pic

To remove duplicates from a list in Python, there are several approaches you can take. Here's a detailed explanation of some commonly used methods: 1. Using theset() function: Theset() function can be used to remove duplicates from a list by converting it to a set, which only stores unique elements. However, this method does not preserve the original order of the elements.

1
2
3
4
5
6
7

my_list = [1, 2, 3, 2, 4, 1, 5]

# Remove duplicates using set()
unique_list = list(set(my_list))

print(unique_list)  # Output: [1, 2, 3, 4, 5]

2. Using a loop and a new list: You can iterate over the original list and add each element to a new list only if it hasn't been added before.

1
2
3
4
5
6
7
8
9

my_list = [1, 2, 3, 2, 4, 1, 5]
unique_list = []

for element in my_list:
    if element not in unique_list:
   unique_list.append(element)

print(unique_list)  # Output: [1, 2, 3, 4, 5]

3. Using a list comprehension: List comprehensions provide a concise way to create new lists. You can use a list comprehension with anif condition to filter out duplicates.

1
2
3
4
5

my_list = [1, 2, 3, 2, 4, 1, 5]
unique_list = [element for i, element in enumerate(my_list) if element not in my_list[:i]]

print(unique_list)  # Output: [1, 2, 3, 4, 5]

4. Using thedict.fromkeys() method: You can utilize thedict.fromkeys() method to remove duplicates from a list while preserving the order of the elements. This works because dictionaries cannot have duplicate keys.

1
2
3
4
5

my_list = [1, 2, 3, 2, 4, 1, 5]
unique_list = list(dict.fromkeys(my_list))

print(unique_list)  # Output: [1, 2, 3, 4, 5]

These are some of the commonly used methods to remove duplicates from a list in Python. Choose the method that best fits your requirements, taking into consideration factors such as preserving element order and performance.