Skip to content

Instantly share code, notes, and snippets.

@musoftware
Last active October 3, 2025 10:03
Show Gist options
  • Select an option

  • Save musoftware/1d8393aad2818e2fb238b52e5a11903c to your computer and use it in GitHub Desktop.

Select an option

Save musoftware/1d8393aad2818e2fb238b52e5a11903c to your computer and use it in GitHub Desktop.
Removing Duplicates from a List in Python
# The original list containing duplicate elements
original_list = [1, 2, 2, 3, 4, 4, 4, 5, 6, 6]
# Use set() to remove duplicates, then convert it back to a list
unique_list = list(set(original_list))
# Print the new list
print(f"Original List: {original_list}")
print(f"Unique List: {unique_list}")
# Output:
# Original List: [1, 2, 2, 3, 4, 4, 4, 5, 6, 6]
# Unique List: [1, 2, 3, 4, 5, 6]
# The original list where order matters
original_list = ['apple', 'orange', 'apple', 'banana', 'orange', 'grape']
# Create a dictionary from the list keys (removes duplicates) and get the keys as a list
unique_list_ordered = list(dict.fromkeys(original_list))
# Print the new list to show the order is preserved
print(f"Original List: {original_list}")
print(f"Unique & Ordered: {unique_list_ordered}")
# Output:
# Original List: ['apple', 'orange', 'apple', 'banana', 'orange', 'grape']
# Unique & Ordered: ['apple', 'orange', 'banana', 'grape']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment