How to convert a list to a set in Python

Discover multiple ways to convert a list to a set in Python. Get tips, see real-world examples, and learn how to debug common errors.

How to convert a list to a set in Python
Published on: 
Thu
Feb 12, 2026
Updated on: 
Mon
Apr 13, 2026
The Replit Team

Python developers often convert a list to a set to remove duplicate elements. This simple operation also improves performance for membership tests, which makes it a fundamental skill to master.

In this article, you'll learn several conversion techniques. You'll also find practical tips, see real-world applications, and get debugging advice to help you manage your data structures with confidence.

Basic conversion with the set() constructor

my_list = [1, 2, 3, 2, 1, 4]
my_set = set(my_list)
print(my_set)--OUTPUT--{1, 2, 3, 4}

The most straightforward way to convert a list is by using the built-in set() constructor. When you pass a list to this function, it iterates through the elements and builds a new set.

This process is memory-efficient because it handles deduplication automatically. The key outcomes are:

  • Uniqueness: Any duplicate items from the original list are discarded. In the example, the numbers 1 and 2 appear only once in the final set.
  • Unordered: The resulting set doesn't maintain the original order of the list's elements.

Basic set conversion techniques

While the set() constructor is a great starting point, Python also provides set comprehensions, the unpacking operator *, and frozenset() for more nuanced conversions.

Using set comprehension

my_list = [1, 2, 3, 2, 1, 4]
my_set = {item for item in my_list}
print(my_set)--OUTPUT--{1, 2, 3, 4}

A set comprehension offers a concise, Pythonic way to create a set from an iterable. The syntax {item for item in my_list} should feel familiar if you've used list comprehensions. The key difference is the curly braces {}, which tell Python to build a set, automatically handling deduplication.

While it achieves the same basic result as the set() constructor, a comprehension is more flexible. It’s especially powerful because you can:

  • Transform elements on the fly before they are added to the set.
  • Add a conditional if clause to filter items during creation.

Using the unpacking operator *

my_list = [1, 2, 3, 2, 1, 4]
my_set = {*my_list}
print(my_set)--OUTPUT--{1, 2, 3, 4}

The unpacking operator * offers a modern and highly readable syntax for this conversion. When you place *my_list inside curly braces, Python unpacks every element from the list and uses them to initialize a new set. This method is often preferred for its clean, expressive style.

  • Conciseness: The syntax {*my_list} is the most direct way to create a set from an existing list.
  • Functionality: It works just like the set() constructor, automatically removing duplicates to produce a set of unique items.

Converting with frozenset()

my_list = [1, 2, 3, 2, 1, 4]
my_frozen_set = frozenset(my_list)
print(my_frozen_set)
print(type(my_frozen_set))--OUTPUT--frozenset({1, 2, 3, 4})
<class 'frozenset'>

When you need an immutable set, use the frozenset() constructor. It works just like set() by removing duplicates, but it creates a version of the set that cannot be changed after it’s made.

  • Immutability: Once a frozenset is created, you can't add or remove elements. This makes it a reliable, constant collection.
  • Hashable: Because it's immutable, a frozenset can be used as a key in a dictionary or as an element in another set—something you can't do with a regular set.

Understanding converting sets to lists is also valuable for the reverse operation when you need ordered, mutable collections.

Advanced set conversion techniques

With the basics covered, you can now tackle more advanced conversions, like filtering elements on the fly or transforming them using functions like map(). You might also need to know about appending elements to sets for dynamic set modification.

Converting and filtering in one step

my_list = [1, 2, 3, 2, 1, 4, None, 0, ""]
my_set = {x for x in my_list if x is not None and x != ""}
print(my_set)--OUTPUT--{0, 1, 2, 3, 4}

Set comprehensions are powerful because you can filter elements as you create the set. The if clause in {x for x in my_list if x is not None and x != ""} acts as a gatekeeper, only allowing elements that meet specific criteria to enter the new set.

  • This approach is more efficient than creating a full set and then removing unwanted items later.
  • In this example, the condition ensures that both None values and empty strings are excluded from the final output, resulting in a cleaner dataset.

For alternative approaches to removing duplicates from lists without losing order, consider other techniques beyond set conversion.

Converting lists with duplicate objects

class Person:
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return self.name == other.name

people = [Person("Alice"), Person("Bob"), Person("Alice")]
unique_people = set(people)
print(len(people), "->", len(unique_people))--OUTPUT--3 -> 2

When converting a list of custom objects, a set needs to know how to identify duplicates. To define uniqueness based on an object's attributes instead of its memory address, you must implement two special methods in your class.

  • The __eq__() method tells Python when two objects are considered equal. In this case, two Person objects are the same if their name attributes match.
  • The __hash__() method provides a consistent hash value for equal objects, which is essential for an object to be stored in a set.

By implementing both, you allow the set() constructor to correctly identify and remove the duplicate Person("Alice") instance.

Using map() with set() for transformation

my_list = ["1", "2", "3", "2", "1", "4"]
my_set = set(map(int, my_list))
print(my_set)
print(sum(my_set)) # Can now perform numeric operations--OUTPUT--{1, 2, 3, 4}
10

The map() function is a powerful tool for applying a function, like int(), to every item in a list. When you combine it with set(), you can transform elements and convert them to a set in a single, efficient line. The expression map(int, my_list) creates an iterator that turns each string into an integer on the fly.

  • The set() constructor then consumes this iterator to build the final set.
  • This approach handles both data transformation and deduplication at once.
  • The result is a set of integers, ready for numeric operations like sum().

Move faster with Replit

Replit is an AI-powered development platform that comes with all Python dependencies pre-installed, so you can skip setup and start coding instantly. You can focus on your code without worrying about environment configuration or package management.

While knowing how to convert a list to a set is useful, you can build complete applications faster with Agent 4. It takes your idea and builds a working product by handling the code, databases, and deployment. Instead of piecing together techniques, you can describe the app you want to build:

  • A data cleaning utility that ingests a list of raw sensor readings, filters out invalid entries, and transforms the data into a set of clean integers using map().
  • An analytics tool that processes multiple lists of user IDs, deduplicates them by converting to a set, and calculates the total number of unique visitors.
  • A contact management system that imports a list of custom objects and uses __hash__() and __eq__() to identify and merge duplicates based on an email attribute.

Simply describe your app, and Replit will write the code, test it, and fix issues automatically, all within your browser.

Common errors and challenges

While conversion is often smooth, you'll sometimes face TypeError exceptions from unhashable items or improperly defined custom objects.

Handling mutable objects in set() conversion

A common pitfall is trying to add mutable (changeable) items, like lists or dictionaries, into a set. Because sets require their elements to be hashable—meaning they can't change—this operation will raise a TypeError. The code below shows this error in action.

my_items = [1, 2, [3, 4], 5]
my_set = set(my_items) # This will raise TypeError
print(my_set)

The operation fails specifically because of the nested list [3, 4]. Since a set can't guarantee the uniqueness of an item that can change, it rejects the mutable list. See how to prepare the data for a successful conversion.

my_items = [1, 2, [3, 4], 5]
my_set = {item for item in my_items if isinstance(item, (int, str, tuple))}
print(my_set) # {1, 2, 5}

To fix the TypeError, you can filter out mutable items during conversion. The set comprehension {item for item in my_items if isinstance(item, (int, str, tuple))} uses isinstance() to check each element's type. It only includes items that are instances of immutable types like integers, strings, or tuples. This approach safely skips the unhashable list [3, 4], preventing the error. It's a reliable way to create a set from mixed data types.

Implementing __hash__ and __eq__ for custom objects in sets

When your list contains custom objects, Python defaults to checking memory addresses for uniqueness, not attributes. This means two objects you consider identical will be treated as distinct, and the set conversion won't remove duplicates. See what happens in this example.

class User:
def __init__(self, id, name):
self.id = id
self.name = name

users = [User(1, "Alice"), User(1, "Alice"), User(2, "Bob")]
unique_users = set(users)
print(len(users), "->", len(unique_users)) # 3 -> 3

The set fails to deduplicate because each User instance is a distinct object in memory. Without instructions on how to compare their attributes, Python treats them as unique. The corrected code below shows how to fix this.

class User:
def __init__(self, id, name):
self.id = id
self.name = name

def __hash__(self):
return hash(self.id)

def __eq__(self, other):
if not isinstance(other, User):
return False
return self.id == other.id

users = [User(1, "Alice"), User(1, "Alice"), User(2, "Bob")]
unique_users = set(users)
print(len(users), "->", len(unique_users)) # 3 -> 2

To fix this, you must define how Python should compare your objects. By implementing the __eq__() and __hash__() methods, you give the set the instructions it needs for deduplication.

  • The __eq__() method defines what makes two objects equal. In this case, two User instances are the same if their id attributes match.
  • The __hash__() method provides a consistent hash value based on that same unique identifier, the id.

With both in place, the set can correctly identify and discard duplicates.

Handling type errors with mixed data in sets

A common TypeError arises when a set contains mixed data types, such as both integers and strings. While the set itself can hold them, operations like sum() will fail because Python can't perform math on incompatible types. The code below demonstrates this issue.

data = [1, "2", 3, "4"]
total = sum(set(data)) # This will raise TypeError
print(total)

The sum() function fails when it tries to add an integer to a string, like 1 + "2". This type mismatch is what causes the TypeError. The following code demonstrates how to properly prepare the data before summing.

data = [1, "2", 3, "4"]
numeric_set = {int(x) for x in data}
total = sum(numeric_set)
print(total) # 10

The fix is to ensure all elements share a numeric type before you perform a calculation. A set comprehension like {int(x) for x in data} iterates through the list, applies the int() function to each item, and builds a new set containing only integers. This standardizes the data, allowing functions like sum() to execute without a TypeError. Be mindful of this whenever you run aggregate operations on sets with mixed data types.

Real-world applications

With the conversion methods and error fixes covered, you can now use sets to solve practical data challenges like finding unique or common elements through AI coding.

Finding unique elements while preserving order with dict.fromkeys()

When preserving the original order of elements is as important as removing duplicates, the dict.fromkeys() method provides a concise way to achieve both.

# Real-world scenario: Preserving order of unique user IDs
user_logs = [101, 102, 101, 103, 102, 104, 101]
unique_ordered_users = list(dict.fromkeys(user_logs))
print(f"Original logs: {user_logs}")
print(f"Unique ordered users: {unique_ordered_users}")

This technique leverages two key features of Python dictionaries. The dict.fromkeys() method creates a dictionary from user_logs, where each item becomes a key. If you're new to dictionaries, learning about creating dictionaries in Python will help you understand this approach better.

  • Deduplication: Dictionary keys must be unique, so duplicates are automatically removed.
  • Order Preservation: Since Python 3.7, dictionaries remember insertion order.

Finally, converting the dictionary back to a list gives you a sequence of unique items that maintains the order of their first appearance. It's an efficient trick for ordered deduplication.

Using set operations for finding common elements in datasets

Sets provide highly efficient methods like intersection() and subtraction (-) to quickly identify what two collections have in common and what makes them unique.

# Finding common interests between two users
user1_interests = {"python", "data science", "hiking", "music"}
user2_interests = {"java", "python", "gaming", "music"}

common_interests = user1_interests.intersection(user2_interests)
unique_to_user1 = user1_interests - user2_interests

print(f"Common interests: {common_interests}")
print(f"Interests unique to user1: {unique_to_user1}")

This example demonstrates how to compare two sets to find shared and distinct items. The intersection() method identifies all elements that exist in both user1_interests and user2_interests, resulting in a new set of common interests.

  • The set subtraction operator (-) finds elements that are in the first set but not in the second.
  • This operation is not symmetrical—user1_interests - user2_interests yields a different result than the reverse. It's a quick way to see what's unique to one collection compared to another.

Beyond finding differences, you might also need to learn about merging two sets to combine multiple datasets.

Get started with Replit

Now, turn your knowledge into a real tool with Replit Agent. Describe what you want: "a tool to find common tags between two lists" or "an app that shows unique user IDs in their original order."

Replit Agent writes the code, tests for errors, and deploys your app. It handles the entire development process, so you can focus on your project. Start building with Replit.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.