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.
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 setfruitList = set(fruitList)# now convert set back into listfruitList = list(fruitList)print("list without duplicates and altered order:\n", fruitList)
for
loopTo 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 listoriginalList = [3, 2, 5, 4, 1, 4, 2]print("Original list:\n", originalList)# delcare new, empty listnewList = []for i in originalList: # traverse original listif i not in newList: # if element was not previously added in new listnewList.append(i) # add the element in new listprint("New list, without duplicates:\n", newList)
Related articles