Convert a List to a Set in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Convert a List to a Set in Python #

Use the set() class to convert a list to a set, e.g. my_set = set(my_list). The set() class can be passed an iterable, such as a list, and returns a new Set object with elements taken from the iterable.

main.py
my_list = ['bobby', 'hadz', 'com', 'com'] my_set = set(my_list) print(my_set) # 👉️ {'com', 'bobby', 'hadz'}

We used the set() class to convert a list to a set object.

Set objects are an unordered collection of unique elements, so any duplicate elements get removed when converting to a set.

The set() class takes an iterable optional argument and returns a new set object with elements taken from the iterable.

If you need to add the items of a list to an existing set, use the set.update() method.

main.py
my_list = ['bobby', 'hadz', 'com', 'com'] my_set = set() my_set.update(my_list) print(my_set) # 👉️ {'bobby', 'com', 'hadz'}
The set.update() method can be passed one or more iterables and updates the set, adding the elements from the iterables.

Alternatively, you can use the iterable unpacking operator.

Convert a List to a Set using the iterable unpacking operator #

Use the iterable unpacking operator to convert a list to a set, e.g. my_set = {*my_list}. The iterable unpacking operator will unpack the items of the list into a set object.

main.py
my_list = ['bobby', 'hadz', 'com', 'com'] my_set = {*my_list} print(my_set) # 👉️ {'hadz', 'com', 'bobby'}

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

You can imagine that the items of the list get passed as multiple, comma-separated elements to the set object.

You can use this approach to convert multiple lists to a set.

main.py
my_list = ['bobby', 'hadz', 'com', 'com'] my_list_2 = ['a', 'b', 'c'] my_set = {*my_list, *my_list_2} print(my_set) # 👉️ {'bobby', 'hadz', 'b', 'a', 'com', 'c'}

Alternatively, you can use a set comprehension.

Convert a List to a Set using a set comprehension #

To convert a list to a set:

  1. Use a set comprehension to iterate over the list.
  2. On each iteration, return the list element.
  3. The new set will contain all of the unique elements of the list.
main.py
my_list = ['bobby', 'hadz', 'com', 'com'] my_set = {element for element in my_list} print(my_set) # 👉️ {'hadz', 'bobby', 'com'}

We used a set comprehension to iterate over the list.

Set comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we simply return the current element.

The new set contains all of the unique elements of the list.

Alternatively, you can use a for loop.

Convert a List to a Set using a for loop #

To convert a list to a set:

  1. Declare a new variable that stores an empty set.
  2. Use a for loop to iterate over the list.
  3. Use the set.add() method to add each item of the list to the set.
main.py
my_list = ['bobby', 'hadz', 'com', 'com'] my_set = set() for item in my_list: my_set.add(item) print(my_set) # 👉️ {'com', 'bobby', 'hadz'}

Make sure to use the set() class when declaring an empty set.

If you use an empty set of curly braces, e.g. a = {}, you would declare a dictionary.

We used a for loop to iterate over the list and use the set.add() method to add each element to the set.

The set.add method adds the provided element to the set.

main.py
my_set = set() my_set.add('bobby') my_set.add('hadz') my_set.add('com') print(my_set) # 👉️ {'com', 'hadz', 'bobby'}

Which approach you pick is a matter of personal preference. I'd go with directly using the set() class if I don't have an existing set.

If I need to add the elements of a list to an existing set, I'd use the set.update() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.