Python Practice › Set Programs › Program 13

Set to a list

EasySet Programs · 13 of 48

Write a Python program to convert a set to a sorted list.

Advertisement
Input
{"python", "java", "go"}
Output
['go', 'java', 'python']

Approach

  1. sorted() returns a list in a predictable order.

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment