Ads by ProfitSence
Close

Python remove duplicates from list Code Example

Last Updated on Wednesday 5th Oct 2022

remove duplicates from list python

Example 1

			
					from collections import OrderedDict

alpList = ["a", "a", "a", "b", "c"]
alpList = list(OrderedDict.fromkeys(alpList))

print(alpList) # ['a', 'b', 'c']

			
	

Example 2 Using sets

  • A set data type cannot store duplicates. So you can also use this method to remove duplicates from a list.

  • Be careful; the set() will change the order of the elements in the list.

			
					from typing import List

numbers: List[int] = [1,1,2,2,3,4,4,4,5,5,6,6,7,8,8]

numbers = list(set(numbers))
print(numbers) # [1, 2, 3, 4, 5, 6, 7, 8]

			
	

Example 3 - Using List Comprehension

You Can refer here

			
					original = [1, 2, 3, 4, 5, 6, 5, 6, 4, 7, 8, 9, "a", "c", "b", "d", "c", "d"]

temp = []
[temp.append(i) for i in original if i not in temp]

print(temp) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'c', 'b', 'd']

			
	
python remove duplicates from list
remove duplicates from list python
python list remove duplicates
list remove duplicates python
how to remove duplicates from list in python
remove duplicates list python
python deduplicate list