Python Practice › Set Programs › Program 13
Set to a list
Write a Python program to convert a set to a sorted list.
Input
{"python", "java", "go"}Output
['go', 'java', 'python']
Approach
sorted()returns a list in a predictable order.
Solution
langs = {"python", "java", "go"}print(sorted(langs))Output
['go', 'java', 'python']
Line by line
Line 2
list(langs) also works, but the order of string items can change between runs; sorted fixes it.
Learn the concept
New to this topic? Read the lesson, then come back.