• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert List to Set with Python

Convert List to Set with Python

February 21, 2022 Leave a Comment

In Python, the easiest way to convert a list to a set is using the Python set() function.

l = [0,1,2,3]

converted_to_set = set(l)

print(converted_to_set)

#Output:
{0,1,2,3}

If your list has duplicates, set() will remove them.

l = [0,1,2,3,0,0,3]

converted_to_set = set(l)

print(converted_to_set)

#Output:
{0,1,2,3}

When working with collections of items in Python, it can be easier to convert them to other data structures to be able to work more efficiently.

We can convert lists to sets in Python easily.

A set is an unordered collection of unique elements. On the other hand, lists are ordered and can contain duplicates. Converting a list to a set creates a new set with the same items as the list and removes all duplicates removed.

To convert a list to a set using Python, we can use the set() function.

Below is an example of using the set() function to convert a list to a set.

l = [0,1,2,3]

converted_to_set = set(l)

print(converted_to_set)

#Output:
{0,1,2,3}

As mentioned above, sets do not allow for duplicate elements. Below is an example of how the set() function will remove duplicates when you convert a list to a set.

l = [0,1,2,3,0,0,3]

converted_to_set = set(l)

print(converted_to_set)

#Output:
{0,1,2,3}

Using a Loop to Convert List to Set in Python

We can also use loops to convert different collection of objects to a new data structure. With Python, we can convert lists to sets using loops easily.

Below is an example of how to us a for loop to convert a list to a set with Python.

l = [0,1,2,3]

new_set = set()

for x in l:
    new_set.add(x)

print(new_set)

#Output:
{0,1,2,3}

How to Convert a Set to a List in Python

If you want to do the reverse and convert a set to a list, you can use the Python list() function.

To convert a set to a list, we just pass the set variable to the list() function.

Below is an example in Python of how to convert a set to a list.

s = {0,1,2,3}

converted_to_list = list(s)

print(converted_to_list)

#Output:
[0,1,2,3]

Hopefully this article has been useful to convert lists to sets in your Python code.

Other Articles You'll Also Like:

  • 1.  Python turtle Colors – How to Color and Fill Shapes with turtle Module
  • 2.  How to Group By Columns and Find Maximum in pandas DataFrame
  • 3.  Remove None From List Using Python
  • 4.  Python Get Operating System Information with os and platform Modules
  • 5.  pandas nlargest – Find Largest Values in Series or Dataframe
  • 6.  Python Remove First Element from List
  • 7.  Get All Substrings of a String in Python
  • 8.  Get Days of timedelta Object in Python
  • 9.  Check if File Exists in AWS S3 Bucket Using Python
  • 10.  Get pandas Index Values as List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy