How to Remove Duplicates From a Python List
Removing duplicates from a list in Python can be achieved using various approaches. Here are five examples along with step-by-step explanations and their outputs:
Example 1: Using a loop and a new list
def remove_duplicates(input_list):
output_list = []
for item in input_list:
if item not in output_list:
output_list.append(item)
return output_list
# Example usage
input_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(input_list)
print(result)
Explanation:
- Define a function
remove_duplicates
that takes an input list. - Initialize an empty list
output_list
. - Iterate through each item in the input list.
- If the item is not already in the output list, append it.
- Return the output list.
Output:
[1, 2, 3, 4, 5]
Example 2: Using set() function
def remove_duplicates(input_list):
return list(set(input_list))
# Example usage
input_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(input_list)
print(result)
Explanation:
- Define a function
remove_duplicates
that takes an input list. - Convert the input list to a set, which automatically removes duplicates.
- Convert the set back to a list and return it.
Output:
[1, 2, 3, 4, 5]
Example 3: Using list comprehension
def remove_duplicates(input_list):
return [item for index, item in enumerate(input_list) if item not in input_list[:index]]
# Example usage
input_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(input_list)
print(result)
Explanation:
- Define a function
remove_duplicates
that takes an input list. - Use list comprehension to iterate through each item and filter out duplicates by checking if the item appears before the current index.
Output:
[1, 2, 3, 4, 5]
Example 4: Using OrderedDict
from collections import OrderedDict
def remove_duplicates(input_list):
return list(OrderedDict.fromkeys(input_list))
# Example usage
input_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(input_list)
print(result)
Explanation:
- Import
OrderedDict
from thecollections
module. - Define a function
remove_duplicates
that takes an input list. - Create an ordered dictionary from the input list, which automatically removes duplicates.
- Convert the ordered dictionary back to a list and return it.
Output:
[1, 2, 3, 4, 5]
Example 5: Using a loop and the count() method
def remove_duplicates(input_list):
output_list = []
for item in input_list:
if input_list.count(item) > 1:
input_list.remove(item)
return input_list
# Example usage
input_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(input_list)
print(result)
Explanation:
- Define a function
remove_duplicates
that takes an input list. - Iterate through each item in the input list.
- If the count of the item is greater than 1 (meaning it is a duplicate), remove it from the input list.
- Return the modified input list.
Output:
[1, 2, 3, 4, 5]
These examples provide different ways to remove duplicates from a list in Python. Choose the one that best fits your requirements and preferences.
Top comments (0)