Skip to content

Python set to list | Conversion example code

  • by

You can use the list() Function or manual Iteration to convert the set into the list in Python.

Python set to list example code

Simple example code.

Using list() Function

It is a built-in method that takes an iterable as an argument and converts that into a List type object. In this way, the order of the list can be random.

my_set = set({1, 4, 3, 2})

my_list = list(my_set)

print(my_list)

Output:

Python set to list

Using Manual Iteration

Manually append the elements to the list since the set is iterable.

my_set = set({1, 4, 3, 2})

my_list = []

for i in my_set:
    my_list.append(i)

print(my_list)

Output: [1, 2, 3, 4]

How to Convert a frozenset to a list in Python?

Answer: The Python frozenset object is similar to a set but is an immutable data type. So you can’t modify the elements of a frozenset. But using the list() function can convert it into a list.

my_set = frozenset({1, 3, 2, 5})

a = list(my_set)

print(a)

Output:

[1, 2, 3, 5]

Do comment if you have any doubts or suggestions on this Python set tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading