# How to Remove Duplicates from a List in Python

09/10/2021

Contents

In this article, you will learn how to remove duplicates from a list in Python.

## Remove Duplicates from a List

To remove duplicates from a list in Python, you can use a set. A set is an unordered collection data type that is iterable, mutable and has no duplicates. By converting a list to a set, you can easily remove duplicates.

Here’s an example:

``````def remove_duplicates(list_with_duplicates):
return list(set(list_with_duplicates))

list_with_duplicates = [1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list_without_duplicates = remove_duplicates(list_with_duplicates)

print(list_without_duplicates)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``````

Note that converting a list to a set and back to a list changes the order of the elements. If the order of the elements is important, you can use a loop to iterate over the list and add each element to a new list only if it has not been added before.

Here’s an example:

``````def remove_duplicates(list_with_duplicates):
list_without_duplicates = []
for item in list_with_duplicates:
if item not in list_without_duplicates:
list_without_duplicates.append(item)
return list_without_duplicates

list_with_duplicates = [1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list_without_duplicates = remove_duplicates(list_with_duplicates)

print(list_without_duplicates)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``````