Remove Duplicate Items From a Python List
This program removes all duplicate items from a Python list by converting it to dictionary and then back to list.
Python Source Code
# Original list
list_with_duplicates = [2,4,5,6,7,2,3,5]
# Removing
list_with_no_duplicates = list(dict.fromkeys(list_with_duplicates))
# Printing
print(list_with_no_duplicates)
Output
[2, 4, 5, 6, 7, 3]
Explanation
In the above program, we create a dictionary using the list item as keys. Since keys are unique in dictionary, duplicate items will not be included in the dictionary which removes all duplicates in the List. This is done by using code dict.fromkeys(list_with_duplicates
Finally we convert dictionary back to list using list()
.
To understand how duplicates are removed from the List, you can look at intermediate results in the following code section:
list_with_duplicates = [2,4,5,6,7,2,3,5]
print('Original list: ',list_with_duplicates)
temp = dict.fromkeys(list_with_duplicates)
print('Dictionary:', temp)
list_with_no_duplicates = list(temp)
print('Back to list: ', list_with_no_duplicates)
Output of the above program is:
Original list: [2, 4, 5, 6, 7, 2, 3, 5] Dictionary: {2: None, 4: None, 5: None, 6: None, 7: None, 3: None} Back to list: [2, 4, 5, 6, 7, 3]