Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to remove duplicates from a list in Python

Sarah Tanveer

In Python, lists are data structures that may contain duplicate elements. In some cases, these duplicate elements may be needed. However, in other cases, we may need to create a unique list from a list with duplicate elements. This can be achieved through the following methods.

1. Use set()

This is a simple way to remove duplicates from a list. Sets are data structures that cannot contain any duplicate elements. When you convert a list into a set, all the duplicates will be removed. The set can then be converted back into a list with list().

The drawback of this method is that the use of set() also changes the original list order, which is not restored after it is converted back into a list.

# delcare list with duplicate elements:
fruitList = ["banana", "orange", "apple", "peach", "apple"]
print("original list:\n", fruitList)

# first convert list into set
fruitList = set(fruitList)

# now convert set back into list
fruitList = list(fruitList)
print("list without duplicates and altered order:\n", fruitList)

2. Use for loop

To remove duplicates, the list can be traversed with a for loop, and the elements can be added into a new list by excluding the duplicates.

This method may be useful if the original list with the duplicates needs to be preserved. This will also ensure that the original order of elements is not changed in the new list.

# original list
originalList = [3, 2, 5, 4, 1, 4, 2]
print("Original list:\n", originalList)

# delcare new, empty list
newList = []

for i in originalList:  # traverse original list
  if i not in newList:  # if element was not previously added in new list
    newList.append(i)  # add the element in new list

print("New list, without duplicates:\n", newList)



View all Courses

Keep Exploring