In this Python tutorial, we’ll learn how to convert Python set to list. We’ll be using 4 easy methods to convert set into list. We’ll understand it using practical Python code examples.
After reading this article, you’ll have a detailed knowledge of how to easily make use of these methods in order to convert Python set into list.
Outline
- Introduction: Python Set To List
- Implementing Python Set To List Conversion (4 Easy Methods)
- Using list() Function
- Using list() Function inside Custom Function
- Using [*set,] Method
- Using sorted() Function
- Conclusion
Introduction: Python Set To List
As the name suggests, it specifies the conversion of Python set into Python list.
- We’ll first use a list function to convert set into list.
- In our second approach, we’ll be creating a function which will do this conversion. Benefit of that function is that we can do this conversion multiple times and with less code.
- In our third approach, we’ll be using [*set,] method.
- Finally, we’ll be using the sorted() function to convert set to list in Python.
Let’s now understand all these four methods using practical Python code examples.
Implementing Python Set To List Conversion (4 Easy Methods)
We’ll uncover these four methods through which the conversion of set to list will take place. We strongly recommend you to pay attention to every detail in order for better understanding. These four methods are listed below:
- Using list() Function
- Using list() Function inside Custom Function
- Using [*set,] Method
- Using sorted() Function
Using list() Function
setVals = {4,6,3,'Zee',4.7,2.5,4,5,6} listVals = list(setVals) print( listVals )
Output
[2.5, 3, 4, 4.7, 6, 5, 'Zee']
We’ve created a set having values of different types. After that we’ve used the list function and have passed the set to it. As a result, we have successfully converted our Python set to list. We can also see that the list is sorted automatically.
Using list() Function inside Custom Function
Let’s now create a custom function which will take a set as an argument, convert it to a list and return it. See below code:
def setToListFun(setVal): listVal = list(setVal) // we can also use one line like this: return list(setVal) return listVal returnedVal = setToListFun( {5,3,6,26,8,3,9} ) print(returnedVal)
Output
[3, 5, 6, 8, 9, 26]
We can reuse this function again and again for more conversion of set into list.
Using [*set,] Method
setVal = {4,3,6,2,'Zee',34.7} convertedVal = [*setVal,] print( convertedVal )
Output
[2, 3, 4, 'Zee', 6, 34.7]
We can use this method as well to convert Python set to list. We can use it inside a custom function as well to make it easily reusable.
Using sorted() Function
setValues = {4,7,2,5,1,7,2.9} convertedValues = sorted(setValues) print( convertedValues )
Output
[1, 2, 2.9, 4, 5, 7]
We can use this function as well. But it will give error if the set consist of items of different types or the types that are not supported to be sorted. See below examples:
{4,7,'Zee'} // bad {4,7,[3,4,4]} // bad {[2,3],[4,8]} // bad
You can try it with other types as well.
So this is how we can easily convert Python set to list. Hope you like this article and have learnt a lot from it. Do feel free to share this post with other Python programmers. We’d be happy to have your feedback on this article.
Also, don’t hesitate to ask if you still have any questions related to the conversion of Python set to list. We’ll be more than happy to answer all your questions.
Conclusion
As a conclusion of this tutorial, hope you now have an in-depth practical knowledge of how to properly convert Python set to list through multiple methods. We’d be very glad to receive your valuable feedback on this post.
Do visit our other posts on Python programming in which we have discussed important concepts of Python in detail using proper practical code examples. Thank you for giving your valuable time to this Python article.