Assignment 26
Assignment 26
Python Program:
# Function to remove duplicates from a list using set
def remove_duplicates(input_list):
print(f"Original list: {input_list}")
unique_set = set(input_list) # Convert list to set
unique_list = list(unique_set) # Convert set back to list
print(f"List after removing duplicates: {unique_list}")
return unique_list
# Test case
input_list = [1, 2, 3, 2, 4, 1, 5, 4, 6]
remove_duplicates(input_list)
Example Execution:
Input:
input_list = [1, 2, 3, 2, 4, 1, 5, 4, 6]
Output:
Original list: [1, 2, 3, 2, 4, 1, 5, 4, 6]
List after removing duplicates: [1, 2, 3, 4, 5, 6]
Considerations:
If maintaining the original order is important, sets won't work because they are
unordered.
To preserve order, a different approach involving a loop and a temporary set to track
seen elements may be required.