How to convert a set to a list in Python
Learn how to convert a Python set to a list. This guide covers various methods, tips, real-world uses, and common error debugging.

You often need to convert a Python set to a list. This common task transforms an unordered collection of unique items into an ordered sequence that can hold duplicates.
In this article, you'll explore several conversion techniques with practical examples. You'll also find real-world applications, performance tips, and advice to debug common issues you might face.
Basic conversion with list() constructor
my_set = {1, 2, 3, 4, 5}
my_list = list(my_set)
print(my_list)--OUTPUT--[1, 2, 3, 4, 5]
The list() constructor is the most Pythonic and readable way to perform this conversion. It’s a built-in function that creates a new list by iterating over the elements of an iterable—in this case, your set.
This method is clean and efficient. Here’s what’s happening under the hood:
- A new list object is created in memory, leaving your original set untouched.
- The order of elements in the new list depends on the set's internal hash-based ordering, which isn't guaranteed to match the order in which you added the items.
Alternative conversion methods
While the list() constructor is the standard approach, you can also use list comprehensions, the unpacking operator *, or even the sum() function for more specialized conversions.
Using list comprehension for set to list conversion
my_set = {1, 2, 3, 4, 5}
my_list = [item for item in my_set]
print(my_list)--OUTPUT--[1, 2, 3, 4, 5]
A list comprehension offers a concise and readable way to build a new list. The expression [item for item in my_set] explicitly iterates over each item in the set and adds it to the new list, making the operation very clear.
- While slightly more verbose than the
list()constructor, this method provides far greater flexibility. - You can easily add conditional logic to filter items or apply transformations during the conversion—something the basic constructor can't do on its own.
Using the unpacking operator * for conversion
my_set = {1, 2, 3, 4, 5}
my_list = [*my_set]
print(my_list)--OUTPUT--[1, 2, 3, 4, 5]
The unpacking operator *, also known as the splat operator, offers a modern and visually clean syntax for this conversion. When you write [*my_set], you're telling Python to take all the items out of my_set and place them individually inside a new list.
- This method is often favored for its conciseness and is functionally equivalent to the
list()constructor. - You can also use it to easily combine multiple sets or other iterables into one list, for example:
[*set1, *list2].
Using the sum() function with nested lists
my_set = {1, 2, 3, 4, 5}
my_list = sum([[x] for x in my_set], [])
print(my_list)--OUTPUT--[1, 2, 3, 4, 5]
This is a clever but unconventional method. It leverages the sum() function, which can concatenate sequences. The expression [[x] for x in my_set] first creates a list of lists, where each element from the original set is wrapped in its own list.
- The
sum()function then iterates through this list of lists, "adding" each one to the provided starting value—an empty list[]. - Since the addition operator
+means concatenation for lists, it effectively flattens the structure into a single list. It’s a neat trick, but far less efficient and readable than the other methods.
Advanced techniques
While the basic methods are great for simple conversions, you'll often need to sort, filter, or apply custom ordering as you create your list.
Converting a set to a sorted list
my_set = {5, 3, 1, 4, 2}
my_sorted_list = sorted(my_set)
print(my_sorted_list)--OUTPUT--[1, 2, 3, 4, 5]
When you need an ordered list from a set, the sorted() function is your go-to tool. It directly converts any iterable into a new, sorted list, which is perfect since sets are inherently unordered.
- Unlike the
list()constructor,sorted()guarantees the output list's elements will be in ascending order. - It creates a brand new list, so your original
my_setremains completely unchanged by the operation.
Converting a set to a list with filtering
my_set = {1, 2, 3, 4, 5, 6}
filtered_list = list(filter(lambda x: x % 2 == 0, my_set))
print(filtered_list)--OUTPUT--[2, 4, 6]
You can filter elements from a set during conversion by wrapping a filter() call inside the list() constructor. This approach lets you apply a condition to each item before it's added to the new list.
- The
filter()function takes two arguments: a function that returns a boolean and your set. - In this case, a
lambdafunction—lambda x: x % 2 == 0—serves as the filter. It’s a small, anonymous function that checks if each number is even. list()then consumes the iterator returned byfilter()to create a list containing only the elements that passed the test.
Converting a set to a list with custom ordering
my_set = {'apple', 'banana', 'cherry', 'date'}
custom_ordered = sorted(my_set, key=len, reverse=True)
print(custom_ordered)--OUTPUT--['banana', 'cherry', 'apple', 'date']
The sorted() function becomes even more powerful when you use its optional parameters. You can create a custom sort order by specifying a function for the key argument.
- In this example,
key=leninstructs Python to sort the strings by their length rather than their alphabetical content. - Adding
reverse=Trueflips the default ascending order, arranging the elements from longest to shortest. This gives you precise control over the final list.
Move faster with Replit
Replit is an AI-powered development platform that transforms natural language into working applications. You can describe what you want to build, and Replit Agent creates it—complete with databases, APIs, and deployment.
For the conversion techniques we've explored, Replit Agent can turn them into production-ready tools. Instead of just converting a set to a list, you can build entire applications that leverage this functionality.
- Build a tag management utility that collects unique tags from user input and displays them as an alphabetized list using
sorted(). - Create a user segmentation tool that filters a set of customer IDs based on specific criteria—like activity status or purchase history—and outputs a clean list for a marketing campaign.
- Deploy a content prioritization dashboard that takes a set of article headlines and sorts them by length using a custom
key, helping editors quickly identify which titles need revision.
Describe your app idea, and Replit Agent will write the code, test it, and fix issues automatically, all within your browser.
Common errors and challenges
Converting sets to lists is usually straightforward, but a few common pitfalls can trip you up if you're not careful.
- Avoiding confusion between empty
{}andset() - When you need an empty set, always use the
set()constructor. It’s a common mistake to use empty curly braces{}, but that syntax actually creates an empty dictionary. This distinction is crucial because it can lead to unexpected behavior and errors later in your code. - Dealing with
TypeErrorwhen sorting mixed-type sets - Sets can hold items of different data types, like numbers and strings. However, this flexibility can cause a
TypeErrorwhen you try to sort the resulting list. Thesorted()function fails because Python doesn't have a default way to compare a string like'apple'to a number like5. - Handling set-to-list conversion with mutable elements
- You might wonder how to handle sets with mutable elements like lists, but here's the catch—you can't. Sets require their items to be "hashable," which means they must be immutable. Trying to add a list or another mutable type to a set will raise a
TypeErrorfrom the start, so this issue is prevented before you even get to the conversion step.
Avoiding confusion between empty {} and set()
It's a classic Python gotcha. While curly braces create sets with elements, using empty braces {} doesn't give you an empty set—it creates an empty dictionary. This subtle difference can lead to unexpected behavior. The following code demonstrates this mix-up in action.
empty_set = {} # This is actually an empty dictionary!
empty_list = list(empty_set)
print(f"Type of empty_set: {type(empty_set)}")
print(f"Converted list: {empty_list}")
The code runs without issue, but the type of empty_set is incorrect. This subtle bug can cause problems later when you try to use set-specific methods. The following example shows how to initialize it correctly.
empty_set = set() # Correct way to create an empty set
empty_list = list(empty_set)
print(f"Type of empty_set: {type(empty_set)}")
print(f"Converted list: {empty_list}")
The correct way to initialize an empty set is with the set() constructor. This ensures you create an object of type set, which you can then convert to an empty list as expected.
This distinction is critical. If you accidentally use {}, you'd have a dictionary, and attempting to use set-specific methods like .add() would raise an error. Always use set() when you need an empty set to prevent these subtle bugs.
Dealing with TypeError when sorting mixed-type sets
Sets can hold mixed data types, but this flexibility creates a problem for the sorted() function. Python can't compare items like a string and a number without a clear rule, so it raises a TypeError. The following code shows this issue.
mixed_set = {1, "apple", 2.5, "banana"}
sorted_list = sorted(mixed_set) # Will raise TypeError
print(sorted_list)
The sorted() function fails because it doesn't know how to compare different data types. It can't determine if 1 should come before or after "apple", so it raises a TypeError. The code below shows one way to fix this.
mixed_set = {1, "apple", 2.5, "banana"}
sorted_list = sorted(list(map(str, mixed_set)))
print(sorted_list)
The fix involves making all elements comparable before sorting. By wrapping the set in map(str, mixed_set), you convert every item—whether it's a number or a string—into a string. Now, the sorted() function can compare them without raising a TypeError. This is a common issue to look for when your sets might gather data of different types, like user input that could be numeric or textual.
Handling set-to-list conversion with mutable elements
You can't add mutable, or changeable, items like a list to a set. Sets require their elements to be "hashable," meaning they must be immutable so their value never changes. Attempting this conversion will fail immediately. The code below demonstrates what happens when you try.
list_of_lists = [[1, 2], [3, 4], [5, 6]]
unique_lists = set(list_of_lists) # TypeError: unhashable type
result = list(unique_lists)
print(result)
The set() constructor fails because it tries to hash each inner list within list_of_lists. Since lists are mutable and therefore unhashable, the operation raises a TypeError. The code below shows how to work around this limitation.
list_of_lists = [[1, 2], [3, 4], [5, 6]]
unique_lists = set(tuple(item) for item in list_of_lists)
result = [list(item) for item in unique_lists]
print(result)
The workaround involves converting each inner list into a tuple. Since tuples are immutable, they're hashable and can be stored in a set. A generator expression, (tuple(item) for item in list_of_lists), efficiently creates these tuples. After the set removes duplicates, a list comprehension converts the unique tuples back into lists. Keep an eye out for this issue when you need to deduplicate collections of lists, like coordinates or data records.
Real-world applications
With those common pitfalls in mind, you can see how these conversions power practical features in real-world applications.
Removing duplicates from user input with set()
Converting a list of user-submitted data to a set and back again is a simple and effective way to remove duplicate entries.
survey_responses = ["red", "blue", "green", "red", "blue", "yellow"]
unique_colors = list(set(survey_responses))
print("All responses:", survey_responses)
print("Unique colors:", unique_colors)
This snippet highlights the interplay between Python's list and set data structures. The code starts with a list called survey_responses that contains multiple occurrences of the same color.
- When you create a
setfrom this list, Python builds a collection where each item can appear only once. - Wrapping this operation in
list()then produces a new list from that collection. The final output shows a list where each color from the original survey appears just one time, though their order isn't guaranteed.
Building a simple recommendation system with set operations and list()
You can leverage set operations like intersection (&) and difference (-) to find commonalities and suggest new items, then convert the results to a list for display.
user1_likes = {"python", "data science", "machine learning", "statistics"}
user2_likes = {"python", "web development", "databases", "machine learning"}
common_interests = user1_likes & user2_likes
recommendations = list(user1_likes - common_interests)
print("Common interests:", list(common_interests))
print("Recommendations for user2:", recommendations)
This code models a simple recommendation engine using two sets of user interests. It leverages powerful set operations to find both shared and unique items before converting the results to lists for display.
- The intersection operator
&findscommon_interestsby creating a new set with only the elements present in bothuser1_likesanduser2_likes. - The difference operator
-then identifies potential recommendations by finding interests that are unique to user 1, which user 2 hasn't seen yet.
Get started with Replit
Put these conversion methods to work by building a real tool. Tell Replit Agent to "build a tag manager that removes duplicate tags and sorts them" or "create a utility that deduplicates a list of email addresses."
The agent writes the necessary code, tests for errors, and handles deployment, turning your idea into a working app. Start building with Replit.
Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.
Create & deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.



%2520in%2520Python.png)