Last modified: Mar 17, 2026 By Alexander Williams

Convert Python List to Set: Remove Duplicates

Python lists and sets are fundamental data structures. They serve different purposes. A list is an ordered collection. It can contain duplicate items. A set is an unordered collection. It only stores unique elements.

Converting a list to a set is a common task. It is often done to remove duplicates. This guide explains the process clearly. It is perfect for beginners.

Understanding Python Lists and Sets

A list is defined with square brackets. It maintains the order of insertion. You can access items by their index.


# Creating a Python list
my_list = [1, 2, 2, 3, 4, 4, 5]
print(my_list)
    

[1, 2, 2, 3, 4, 4, 5]
    

A set is defined with curly braces or the set() constructor. It does not allow duplicate values. The order of elements is not guaranteed. For a deeper dive, see our Python Sets Guide: Unordered Unique Collections.


# Creating a Python set
my_set = {1, 2, 3, 4, 5}
print(my_set)
    

{1, 2, 3, 4, 5}
    

Why Convert a List to a Set?

The primary reason is duplicate removal. It is fast and efficient. Sets automatically enforce uniqueness.

Other reasons include membership testing. Checking if an item is in a set is faster than in a list. This is due to how sets are implemented internally.

You might also need to perform mathematical set operations. These include union, intersection, and difference.

How to Convert List to Set

Use the built-in set() function. Pass your list as the argument. The function returns a new set object.


# Original list with duplicates
fruit_list = ["apple", "banana", "apple", "orange", "banana", "kiwi"]
print("Original List:", fruit_list)

# Convert list to set to remove duplicates
fruit_set = set(fruit_list)
print("Converted Set:", fruit_set)
    

Original List: ['apple', 'banana', 'apple', 'orange', 'banana', 'kiwi']
Converted Set: {'orange', 'banana', 'apple', 'kiwi'}
    

Important: The original list remains unchanged. The set() function creates a new set. The order of items in the output may differ from the list.

Handling Lists with Unhashable Items

Sets can only contain "hashable" items. Immutable types like integers, strings, and tuples are hashable. Mutable types like lists or dictionaries are not.

If your list contains unhashable items, conversion will fail.


# This will cause an error
nested_list = [[1, 2], [3, 4]]
try:
    my_set = set(nested_list)
except TypeError as e:
    print("Error:", e)
    

Error: unhashable type: 'list'
    

To handle this, you must convert inner items to a hashable type first. A common solution is to convert inner lists to tuples.

Converting Back to a List

After removing duplicates, you might want a list again. Use the list() function on the set.


# Set with unique items
unique_fruit_set = {'apple', 'kiwi', 'banana', 'orange'}

# Convert set back to a list
unique_fruit_list = list(unique_fruit_set)
print("New List from Set:", unique_fruit_list)
    

New List from Set: ['orange', 'banana', 'apple', 'kiwi']
    

Remember, the order in the new list is not the original order. If you need to preserve order, consider a different approach.

Preserving Order When Removing Duplicates

Standard set conversion does not keep order. In Python 3.7+, dictionaries preserve insertion order. You can use this to your advantage.


# List with duplicates
ordered_list = ["zebra", "ant", "zebra", "cat", "ant", "dog"]

# Use dict.fromkeys() to remove duplicates while keeping order
ordered_unique_list = list(dict.fromkeys(ordered_list))
print("Order-Preserved List:", ordered_unique_list)
    

Order-Preserved List: ['zebra', 'ant', 'cat', 'dog']
    

This method is efficient and clean. It is a great alternative when order matters.

Practical Examples and Use Cases

Let's look at a more complex example. We will clean user input data.


# Simulating user-entered tags with many duplicates
user_tags = ["python", "code", "beginner", "python", "tutorial", "code", "python"]

print("Raw Tags:", user_tags)

# Get unique tags using a set
unique_tags = set(user_tags)
print("Unique Tags:", unique_tags)

# Convert back to a sorted list for display
sorted_unique_tags = sorted(list(unique_tags))
print("Sorted Unique Tags:", sorted_unique_tags)
    

Raw Tags: ['python', 'code', 'beginner', 'python', 'tutorial', 'code', 'python']
Unique Tags: {'tutorial', 'python', 'code', 'beginner'}
Sorted Unique Tags: ['beginner', 'code', 'python', 'tutorial']
    

This is very useful for data cleaning. For more set-specific code, check out Python Sets Examples: Code for Unique Data.

Performance Considerations

Converting a list to a set is an O(n) operation. It generally scales well with the size of the list. The main cost is computing hashes for each element.

For very large lists, this operation uses extra memory. It creates a completely new set object. Be mindful of your application's memory constraints.

Using a set for membership tests (the `in` keyword) is much faster than a list. This is a key performance benefit.

Common Pitfalls and Tips

1. Loss of Order: The most common surprise. Use `dict.fromkeys()` if you need to keep the order.

2. Unhashable Items: Ensure your list contains only immutable, hashable types.

3. Original List Modification: The `set()` function does not change the original list. You must assign the result to a new variable.

4. Empty Lists: Converting an empty list results in an empty set, which is perfectly valid.

Conclusion

Converting a Python list to a set is simple. Use the built-in set() function. Its main use is efficient duplicate removal.

Remember that sets are unordered and require hashable items. To preserve order, use `dict.fromkeys()`. To get a list back, use the list() function.

This operation is a cornerstone of data cleaning in Python. Mastering it will make your code more efficient. For related setup tasks, you might find our Installing Plone 6: Complete Python Setup Guide helpful.

Start using sets to handle unique data in your projects today.