Convert a List to Set in Python

This article will discuss different ways to convert a list to a set in Python.

Table Of Contents

Suppose we have a list,

[110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

Now we want to convert this list to a set like this,

{96, 97, 100, 110, 89, 91, 92, 93}

Some of the essential points to note here are,

Advertisements
  • A set in Python contains unique elements. So when we convert a list to Python, duplicate elements will not be added to the Set.
  • A set uses hashing internally to filter out duplicate elements. So after converting a list into a set, the order of elements in Set will be different from the list.

Let’s see how to convert a list to set in Python.

Convert a List to a Set using the default constructor

We can pass the list object to Set () constructor to create a Set populated with list contents. For example,

# Create a list of integers
numbers = [110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

# Convert a List to a Set
setOfNumbers = set(numbers)

print(setOfNumbers)

Output:

{96, 97, 100, 110, 89, 91, 92, 93}

It converted the list to a set. Duplicate elements from list were not added to the Set, and also, the order of elements in Set is different from the list.

Convert a List to a set using curly brackets

We can use the * operator with list to unpack the list elements into positional arguments and pass them to the curly brackets i.e. {}. It will create a set object from the list contents. For example,

# Create a list of integers
numbers = [110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

# Convert a List to a Set
setOfNumbers = {*numbers}

print(setOfNumbers)

Output:

{96, 97, 100, 110, 89, 91, 92, 93}

It converted the list to a set. Duplicate elements from list were not added to the set, and also, the order of elements in set is different from the list.

Convert a List to a set using iteration

We can initialize an empty set. Then iterate over the list elements using for loop and add them to the set one by one. For example,

# Create a list of integers
numbers = [110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

# Convert a List to a Set
setOfNumbers = set()
for item in numbers:
    setOfNumbers.add(item)

print(setOfNumbers)

Output:

{96, 97, 100, 110, 89, 91, 92, 93}

This approach has the advantage of filtering out elements if we want based on conditions. For example, let’s convert the list to set but include only greater than 95. For example,

# Create a list of integers
numbers = [110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

# Convert a List to a Set but add 
# items greater than 95 only
setOfNumbers = set()
for item in numbers:
    if item > 95:
        setOfNumbers.add(item)

print(setOfNumbers)

Output:

{96, 97, 100, 110}

This added unique items greater than 95 to the set.

Convert a List to a set using set comprehension

Initialize an empty set. Then iterate over the list elements using set comprehension and add items to set one by one. For example,

# Create a list of integers
numbers = [110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

# Convert a List to a Set
setOfNumbers = {item for item in numbers}

print(setOfNumbers)

Output:

{96, 97, 100, 110, 89, 91, 92, 93}

We can skip certain elements based on conditions while converting the list to set. For example, let’s convert the list to set but include only greater than 95. For example,

# Create a list of integers
numbers = [110, 100, 89, 100, 89, 91, 92, 93, 91, 97, 96, 96, 100, 110]

# Convert a List to a Set but add 
# items greater than 95 only
setOfNumbers = {item for item in numbers if item > 95}

print(setOfNumbers)

Output:

{96, 97, 100, 110}

This added unique items greater than 95 to the set.

Summary:

We learned different ways to convert a list to set in Python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top