How to convert set to list in Python
In this article, we will see how to convert set to list in Python.
Method 1: Using list()
A set can be easily typecast into a list using the list()
function.
sample = {"one", "two", "three"} l = list(sample) print(l)
Output:
['three', 'two', 'one']
Method 2: Using sorted()
We can use the sorted()
function to convert the set into list in a defined order. The elements of the set need to be sortable for using this method.
def setToList(set): return sorted(set) sample = {4, 7, 9, 11} result = setToList(sample) print(result)
Output:
[4, 7, 9, 11]
Subscribe
Login
Please login to comment
0 Discussion