This article will discuss different ways to convert a list to a set in Python.
Table Of Contents
- Convert a List to a Set using the default constructor
- Convert a List to a set using curly brackets
- Convert a List to a set using iteration
- Convert a List to a set using set comprehension
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,
- 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
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
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.