Table of contents
1.
Introduction
2.
How to Convert a List to Set in Python?
2.1.
Using List Comprehension
2.2.
Using map() and set
2.3.
Using a Loop and a Set Constructor
2.4.
Using a Loop and a List Constructor
3.
Frequently Asked Questions
3.1.
What is the difference between a list and a set in Python?
3.2.
Why would I convert a list to a set?
3.3.
Can I convert a set back to a list?
4.
Conclusion
Last Updated: Aug 19, 2025
Easy

How to Convert a List to Set in Python?

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, a list is a collection of ordered elements, which can contain duplicates. A set, on the other hand, is an unordered collection of unique elements. Sometimes, we may want to convert a list to a set to remove duplicates and ensure that the data is unique. 

How to Convert a List to Set in Python?

In this article, we will discuss different methods to convert a list to a set in Python, such as using list comprehension, the map() function, loops, and constructors. 

How to Convert a List to Set in Python?

Converting a list to a set is a common operation in Python, especially when we want to eliminate duplicates from a list. There are several methods to do this, and each method has its own advantages. Let’s walk through each approach with examples.

To convert a list to a set in Python, you can simply pass the list as an argument to the built-in `set()` function. For example:

my_list = [1, 2, 3, 2, 4, 3, 5]
my_set = set(my_list)
print(my_set)
You can also try this code with Online Python Compiler
Run Code


Output:

{1, 2, 3, 4, 5}


As you can see, the `set()` function removes any duplicate elements from the list & returns a new set object. The resulting set is unordered, so the elements may appear in a different order than they were in the original list.

You can also use the `set()` function directly on a list literal to create a set:

my_set = set([1, 2, 3, 2, 4, 3, 5])
print(my_set)
You can also try this code with Online Python Compiler
Run Code


Output:

{1, 2, 3, 4, 5}


This achieves the same result as the previous example.

It's important to note that when you convert a list to a set, any duplicate elements are removed. If you need to preserve the original list, you can create a new set object & keep the original list intact:

my_list = [1, 2, 3, 2, 4, 3, 5]
my_set = set(my_list)
print(my_list)  # Original list is unchanged
print(my_set)   # New set is created
You can also try this code with Online Python Compiler
Run Code


Output:

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


Time Complexity to Convert List to Set:

When converting a list to a set using the set() function, Python needs to iterate over each element in the list & add it to the set. The time complexity of this operation depends on the number of elements in the list.

In the worst case, where all elements in the list are unique, the time complexity of converting a list to a set is O(n), where n is the number of elements in the list. This is because Python needs to process each element once.

However, if the list contains duplicate elements, the time complexity can be slightly higher. When adding elements to a set, Python uses a hash table to ensure uniqueness. In the case of hash collisions, the time complexity of adding an element to the set can be O(1) on average, but in the worst case, it can be O(n).

Overall, the time complexity of converting a list to a set is O(n) on average, where n is the number of elements in the list.

Using List Comprehension

List comprehension is a concise way to create lists in Python. It can also be used to convert a list into a set. The advantage of using list comprehension is its simplicity and readability.

Here's how you can convert a list to a set using list comprehension:

# List of numbers with duplicates
numbers = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9]

# Convert list to set using list comprehension
unique_numbers = {x for x in numbers}

# Print the result
print(unique_numbers)
You can also try this code with Online Python Compiler
Run Code


Explanation:

  • {x for x in numbers} is a set comprehension that iterates over each element of the list numbers and adds it to a set.
     
  • A set automatically removes duplicates, so the result will only contain unique values.
     

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 9}


In this example, you can see that the duplicate values 4 and 7 are removed, and the set contains only unique values.

Using map() and set

The map() function in Python is used to apply a specific function to all the items in an input list. When combined with the set() constructor, it can convert a list to a set. This method can be useful when you want to apply a function to each element before converting it into a set.

Here's an example of converting a list to a set using map() and set():

# List of numbers with duplicates
numbers = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9]

# Convert list to set using map and set
unique_numbers = set(map(int, numbers))

# Print the result
print(unique_numbers)
You can also try this code with Online Python Compiler
Run Code


Explanation:

  • map(int, numbers) applies the int function to each element of the numbers list (though, in this case, all elements are already integers).
     
  • set() then converts the result of map() into a set, removing any duplicates in the process.
     

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 9}

 

In this example, even though the map() function isn't strictly necessary for converting the list to a set, it is included to show how you might apply a function to the list elements before conversion.

Using a Loop and a Set Constructor

If you prefer using explicit loops in your code, you can create a set manually by iterating over the list and adding elements to the set one by one. This method is more flexible and allows for more control over the process.

Here's how to convert a list to a set using a loop and the set() constructor:

# List of numbers with duplicates
numbers = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9]

# Create an empty set
unique_numbers = set()

# Loop through the list and add each element to the set
for num in numbers:
    unique_numbers.add(num)

# Print the result
print(unique_numbers)
You can also try this code with Online Python Compiler
Run Code


Explanation:

  • First, an empty set unique_numbers is created.
     
  • Then, the for loop iterates over each element in the list numbers and adds it to the set using the .add() method.
     
  • Since sets automatically remove duplicates, any repeated values in the list will not be added again.


Output:

{1, 2, 3, 4, 5, 6, 7, 8, 9}


This approach is very straightforward and gives you more control over how each element is processed before adding it to the set.

Using a Loop and a List Constructor

Another way to convert a list to a set is by first using a loop to construct a new list with unique elements and then converting that list to a set. While not as efficient as directly using a set, this method can be useful in certain situations where you might need to filter or modify the elements before converting them.

Here’s an example of using a loop and a list constructor to remove duplicates:

# List of numbers with duplicates
numbers = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9]

# Create an empty list to store unique numbers
unique_numbers_list = []

# Loop through the list and add unique elements to the new list
for num in numbers:
    if num not in unique_numbers_list:
        unique_numbers_list.append(num)

# Convert the list to a set
unique_numbers_set = set(unique_numbers_list)

# Print the result
print(unique_numbers_set)
You can also try this code with Online Python Compiler
Run Code


Explanation:

  • A new list unique_numbers_list is created to hold the elements without duplicates.
     
  • The for loop checks if the current element is not already in the unique_numbers_list. If it's not, it appends the element to the list.
     
  • Finally, the list is converted to a set to ensure that all elements are unique.
     

Output

{1, 2, 3, 4, 5, 6, 7, 8, 9}


This method gives you an opportunity to manually control which elements to keep, but it is less efficient compared to directly converting the list to a set.

Frequently Asked Questions

What is the difference between a list and a set in Python?

A list is an ordered collection of elements, which can contain duplicates. A set is an unordered collection of unique elements, meaning no duplicates are allowed.

Why would I convert a list to a set?

You might convert a list to a set when you want to remove duplicates or ensure that all the elements are unique.

Can I convert a set back to a list?

Yes, you can convert a set back to a list using the list() constructor, like this: list(my_set).

Conclusion

In this article, we discussed different methods to convert a list to a set in Python. These methods include using list comprehension, the map() function, a loop with a set constructor, and a loop with a list constructor. Each method has its own use case, and understanding how to convert a list to a set can be valuable for managing unique elements in your Python code.

Live masterclass