How to Remove Duplicates from a List
Removing duplicates from a list can be easily done by converting the list to a set
, which inherently contains only unique elements, and then converting it back to a list.
Example: Removing Duplicates from a List
# Removing duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers)
Output
[1, 2, 3, 4, 5]
Explanation: By converting the list to a set, we remove duplicate elements. Then, we convert it back to a list to maintain list functionality.