How to Convert a List to a Set in Python?

In this tutorial, I will explain how to convert a list to a set in Python. As a Python developer working on a project for a US-based company, I recently faced a scenario where I needed to efficiently convert a list of employee names to a set to eliminate duplicates. In this post, I’ll share the different methods you can use to achieve this, along with practical examples.

Convert a List to a Set in Python

Before we get into converting a list to a set, let’s quickly recap what lists and sets are in Python.

A list is an ordered collection of items that allows duplicates. In Python, you can create a list using square brackets []. For example:

employee_list = ["John", "Emma", "Michael", "Emily", "John", "David"]

On the other hand, a set is an unordered collection of unique items. It does not allow duplicates. You can create a set using curly braces {} or the set() function. For example:

employee_set = {"John", "Emma", "Michael", "Emily", "David"}

Read How to Get Unique Values from a List in Python?

1. Use the set() Function

The most simple way to convert a list to a set in Python is by using the built-in set() function. The set() function takes an iterable object as an argument and returns a new set containing the unique elements from the iterable

Here’s an example:

employee_list = ["John", "Emma", "Michael", "Emily", "John", "David"]
employee_set = set(employee_list)
print(employee_set)

Output:

{'Michael', 'Emily', 'David', 'Emma', 'John'}

You can see the output in the screenshot below.

Convert a List to a Set in Python

As you can see, the set() function eliminates the duplicate entry of “John” and returns a set with unique employee names.

Check out How to Select Items from a List in Python?

2. Use Set Comprehension

Another way to convert a list to a set is by using set comprehension in Python. Set comprehension is a concise way to create a set based on an existing list.

Here’s an example:

employee_list = ["John", "Emma", "Michael", "Emily", "John", "David"]
employee_set = {name for name in employee_list}
print(employee_set)

Output:

{'David', 'Emma', 'John', 'Michael', 'Emily'}

You can see the output in the screenshot below.

How to Convert a List to a Set in Python

The set comprehension {name for name in employee_list} creates a new set by iterating over each element in the employee_list.

Read How to Add Tuples to Lists in Python?

3. Use the union() Method

Python union() method is used to combine multiple sets into a single set. You can also use it to convert a list to a set by passing the list as an argument to union().

Here’s an example:

employee_list = ["John", "Emma", "Michael", "Emily", "John", "David"]
employee_set = set().union(employee_list)
print(employee_set)

Output:

{'Michael', 'Emily', 'John', 'David', 'Emma'}

You can see the output in the screenshot below.

Convert a List to a Set in Python union() method

In this approach, we start with an empty set set() and use the union() method to add the elements from the employee_list to the set.

Check out How to Convert Dictionary to List of Tuples in Python?

Handle Duplicates and Preserve Order

When converting a list to a set, it’s important to keep in mind that sets are unordered and do not allow duplicates. If you need to preserve the original order of elements from the list, you can convert the set back to a list using the list() function.

employee_list = ["John", "Emma", "Michael", "Emily", "John", "David"]
employee_set = set(employee_list)
unique_employee_list = list(employee_set)
print(unique_employee_list)

Output:

['John', 'Emily', 'Emma', 'Michael', 'David']

In this example, we first convert the employee_list to a set using set() to remove duplicates. Then, we convert the set back to a list using list() to get a list of unique employee names. However, the order of elements in the resulting list may differ from the original list.

Read How to Split a Python List Into Evenly Sized Chunks?

Add Elements to a Set

Once you have converted a list to a set, you may need to add new elements to the set. You can use the add() method to add a single element or the update() method to add multiple elements from an iterable.

employee_set = {"John", "Emma", "Michael", "Emily", "David"}
employee_set.add("Olivia")
employee_set.update(["William", "Ava"])
print(employee_set)

Output:

{'Emily', 'William', 'Emma', 'Ava', 'Michael', 'Olivia', 'David', 'John'}

In this example, we add a single element “Olivia” using the add() method and multiple elements [“William”, “Ava”] using the update() method.

Check out How to Find the Index of the Maximum Value in a List using Python?

Conclusion

In this tutorial, I explained how to convert a list to a set in Python. I discussed three methods to accomplish this task, such as using set() function, using set comprehension , using the union() method. I also covered how to handle duplicates, preserve order, and add elements to a set.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.