Convert Set to List in Python

There are multiple ways to convert a set to a list in python but before discussing these methods let’s first discuss what is set and list in python.

Set in Python

A set in Python is a collection of unordered or non-sequential forms of distinct elements, a set is denoted by curly braces{} in python. A set does not contain any repeated element and it does not support slicing as well.

List in Python

If we talk about the list so, a list in python is a combination of an ordered and sequential group of elements, it is denoted by square brackets [] in python. One more thing is that a list can have repeated elements and it supports slicing.

How to convert Set to List in Python

List Method

Example

``````pyt_set = {'hello', 'world', 'good','morning'}
ps = list(pyt_set)
print(ps)
``````

Code Explanation

In the above example, we declared a variable and stored a set of elements in it and then we used the list function of python which will convert the set into a list, and then we just printed the output, check out the result below.

Output

``````['world', 'hello', 'good', 'morning']
``````

As you can see from the output above, the set has been converted into a list using the list method.

List Comprehension Method

Example

``````# defining set
pyth_set = {12, 12.5, 13, 'hello','world'}

# Using List comprehension method
pylist = [element for element in pyth_set]
print("the set Converted list is:", pylist)
``````

Code Explanation

We declared a variable and stored a set of distinct elements in it then we used the list comprehension method which will create a list by using set values, in the end, we just printed the list. Let’s check out the result below.

Output

``````the set Converted list is: ['hello', 'world', 12, 12.5, 13]
``````

The comprehension method converted the set into a list and it did not just convert the list, it arranged the list elements in a sequential form as well.

For Loop Append Method

Example

``````# delaring set
pyt_set = {11, 12, 'hello' , 13, 14}
# defining empty list
pylist = []
#  using for loop
for element in pyt_set:
# will Add each set element in the list

pylist.append(element)
print("the set Converted list is:", pylist)
``````

Code Explanation

we declared a set and stored distinct elements in a non-sequential order then we declared an empty list, after that, we used for loop and mentioned the set whose elements will be added to the list, and then we used the append method which will add element in the list, in the end, we just printed the output.

Output

``the set Converted list is: ['hello', 11, 12, 13, 14]``

The loop and append method converted the set list and the elements of the list are in sequential order.

Sort Method

Example

``````# defining Set
pyt_set1 = {'y', 'z', 'x'}
pyt_set2 = {11, 12, 13, 14.5, 13.7}

# Using sort Function
pylist1 = sorted(pyt_set1)
pylist2 = sorted(pyt_set2)

print("1st Converted List", pylist1)
print("2nd converted List", pylist2)
``````

Code Explanation

In the above example we declared two unordered sets then we used the sorting method which will sort the elements of sets and will convert them into lists, in the end, we sorted and printed both lists check out the result below.

Output

``````1st Converted List ['x', 'y', 'z']
2nd converted List [11, 12, 13, 13.7, 14.5]
``````

Both sets are converted into ordered lists.

Conclusion

In this tutorial, we talked about the ways for converting a set to a list in python. First, we discussed a short introduction to python, lists, and sets, then we discussed the list function with an example, after that we saw the list comprehension method and practiced an example.

We also used the for loop with the append method and saw its example, in the end, we used the sorting method as well for converting a set to a list.