Python Set to List

Filed Under: Python

Python Set to List

Sometimes we have to create a list from the set object. We can use built-in list() function for this.

Let’s look at some examples to convert set to list in Python.


s = {"A", "B", "C"}

l1 = list(s)

print(type(l1))
print(l1)

s = set()
s.add("A")
s.add("B")

l1 = list(s)

print(l1)

Output:


<class 'list'>
['B', 'A', 'C']
['B', 'A']
Python Set To List

Python Set To List

Python frozenset to list

The frozenset object is an immutable unordered collection of unique elements. So it’s a set that we can’t modify. We can pass a frozenset object to list() function too.


s = frozenset({"A", "B"})

l1 = list(s)

print(l1)

Output: ['B', 'A']

Python list to set

If you need to convert list to set, you can use set() function.


l1 = [1, 2, 3, 2, 1]
s1 = set(l1)
print(type(s1))
print(s1)

Output:


<class 'set'>
{1, 2, 3}
You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: list() Documentation

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages