Python – Convert Set to List

Convert Set to List

Sometimes, because of the differences in how elements are stored in a list, or set, we may need to convert a given Set of elements into a List in Python.

In this tutorial, we shall learn how to convert a given set into a list. There are many ways to do that. And we shall discuss the following methods with examples for each.

  • Use list() builtin function.
  • Use List Comprehension.

1. Convert Set to List using list() builtin function

list() builtin function can take any iterable as argument, and return a list object formed using the elements from the given iterable.

In the following program, we take a set of strings, and convert it into a list.

Python Program

# Take a set of elements
mySet = {'apple', 'banana', 'cherry'}

# Convert set into list
output = list(mySet)
print(f'List : {output}')
Run Code Copy

Output

List : ['apple', 'cherry', 'banana']

2. Convert Set to List using List Comprehension

In the following program ,we use List Comprehension and create a list from the elements of the Set.

Python Program

# Take a set of elements
mySet = {'apple', 'banana', 'cherry'}

# Create list from elements of set
output = [x for x in mySet]
print(f'List : {output}')
Run Code Copy

Output

List : ['banana', 'apple', 'cherry']

Summary

In this tutorial of Python Examples, we learned how to convert a Python Set into a List using different methods, with the help of well detailed example programs.

Related Tutorials

Code copied to clipboard successfully 👍