Python | Remove Duplicates from a List
The job is simple. We need to take a list, with duplicate elements in it and generate another list which only contains the element without the duplicates in them.
Examples:
Input : [2, 4, 10, 20, 5, 2, 20, 4] Output : [2, 4, 10, 20, 5] Input : [28, 42, 28, 16, 90, 42, 42, 28] Output : [28, 42, 16, 90]
We can use not in on list to find out the duplicate items. We create a result list and insert only those that are not already not in.
# Python code to remove duplicate elements def Remove(duplicate): final_list = [] for num in duplicate: if num not in final_list: final_list.append(num) return final_list # Driver Code duplicate = [2, 4, 10, 20, 5, 2, 20, 4] print(Remove(duplicate)) |
chevron_right
filter_none
Output:
[2, 4, 10, 20, 5]
Recommended Posts:
- Python | Remove duplicates from nested list
- Python | Remove consecutive duplicates from list
- Python | Ways to remove duplicates from list
- Python | Sort given list by frequency and remove duplicates
- Python | Remove all duplicates and permutations in nested list
- Remove all duplicates from a given string in Python
- Python | Remove all duplicates words from a given sentence
- Python groupby method to remove all consecutive duplicates
- Python | Merging duplicates to list of list
- Python | Program to print duplicates from a list of integers
- Python | Program to count duplicates in a list of tuples
- Python | Combine two lists by maintaining duplicates in first list
- Python | Remove all values from a list present in other list
- Python list remove()
- Python | Remove None values from list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



