How to convert set to list Python (7 Ways)

python convert set to list

In this article, we will be solving a problem statement for how to convert set to list Python. We will be discussing the steps and python methods required to solve this problem. There are a lot of ways to convert set to list, out of which we will be looking at the top 7 ways.

A list is one of the four in-built data types in Python. A list in Python is a data container that stores data elements, that can be mutable, ordered, can be duplicated within the list. The list is created by placing all the elements within the square brackets and separated by commas.

Example:-

list1 = [1,2,3,'john',7.5,3]

What is a set in Python?

A set is one of the four in-built data types in Python. A set in Python is an unordered, mutable, collection of unique elements. The Set is created by placing all the elements within the curly brackets separated by commas.

Example:-

set1 = {1,2,3,'john',7.5}

Difference between List and Set in Python

List

Set

The list allows duplicate elements

Set doesn’t allow duplicate elements

The list is an ordered sequence

The set is an unordered sequence

The elements in the list are present in square brackets [ ].

The elements in the list are present in curly brackets { }.

Using the list we can implement ArrayList, LinkedList, Vector, Stack

Using the set we can implement HashSet, LinkedHashSet.

How to Convert set to list Python

The 7 Ways to convert set to list in Python are as follows-

Method 1- Convert list to set using Using list() method

The easiest way to convert set to list is by using the python list() method. The list() method takes the iterator such as a set, tuple, dictionary and converts them into a list.

Syntax –

list(iterator)

Iterator – An iterator such as a set, tuple, or dictionary.

Python Code:

Python Code:
# define set
defined_set = {1, 2, 3, 3.5, 'foo'}

# Using list() method to convert set to list
list1 = list(defined_set)
print("Converted list is:", list1)

Output:-

Converted list is: [1, 2, 3.5, 3, ‘foo’]

Method 2 - for loop and append()

By using for loop we will iterator on set and add each element to the list by using the append() method. The append() method adds an element in the existing list at the end of the list.

Syntax for append() –

list.append(element)

Python Code:

# define set
defined_set = {1, 2, 3, 3.5, 'foo'}

# defining empty list
list1 = []

# Iterating set using for loop
for element in defined_set:
    # Adding each element in list
    list1.append(element)

print("Converted list is:", list1)

Output:-

Converted list is: [1, 2, 3.5, 3, ‘foo’]

Method 3- Using list comprehension

In Python list comprehension is used for creating a list based on the values of other iterators such as list, set, tuple, dictionary. To know more about list comprehension you can read here.

Python Code:

# define set
defined_set = {1, 2, 3, 3.5, 'foo'}

# Using List comprehensionhttps://myprogrammingtutorial.com/
list1 = [element for element in defined_set]
print("Converted list is:", list1)

Output:-

Converted list is: [1, 2, 3.5, 3, ‘foo’]

Method 4- Convert set to list using dict.fromkeys()

In this method, we will be using the dictionary fromkeys() method. To know about dict.fromkeys() method you read here.

Python Code:

# defined Set
defined_set = {1, 2, 3, 3.5, 'foo'}
 
# Using dict.fromkey()
list1 = list(dict.fromkeys(defined_set))
print("Converted list is:", list1)

Output:-

Converted list is: [1, 2, 3.5, 3, ‘foo’]

Method 5- Using Map and lambda function

In python lambda function is an anonymous function i.e a function with no name. It is a small function that can have any number of arguments but has only one expression.

Syntax-

lambda argument(s): expression

To know more about the lambda function you can visit here. To convert set to list we used map function with lambda.

Python code:

# defined Set
defined_set = {1, 2, 3, 3.5, 'foo'}

# USing map and lambda function
list1 = list(map(lambda l: l, defined_set))
print("Converted list is:", list1)

Output:-

Converted list is: [1, 2, 3.5, 3, ‘foo’]

Method 6- Unpack set inside the parenthesis

In this method, to convert set to list we unpack the set inside the list literal. We need to use the * symbol which represents all elements in the set.

Python code:

# defined Set
defined_set = {1, 2, 3, 3.5, 'foo'}
 
# Unpack the set
list1 = [*defined_set]
print("Converted list is:", list1)

Output:-

Converted list is: [1, 2, 3.5, 3, ‘foo’]

Method 7- Using sorted() method

We can also use the sorted() method to convert the list to set in python. The only disadvantage of this method is that it returns the ordered list and the set should either have numerical (int, float) values or strings. We cannot store numerical values and strings at the same time

Python code:

# defined Set
defined_set1 = {'b', 'c', 'a'}
defined_set2 = {1, 2, 3, 4.5, 3.7}
 
# Using sorted Function
list1 = sorted(defined_set1)
list2 = sorted(defined_set2)
 
print("Converted List 1", list1)
print("converted List 2", list2)

Output:-

Converted List 1 [‘a’, ‘b’, ‘c’]
converted List 2 [1, 2, 3, 3.7, 4.5]

Conclusion

Hence we have seen how to convert set to list python in 7 different ways. We can convert set to list in python by using list(), for loop, using list comprehension, lambda function, sorted function, and by unpacking set inside parenthesis.

About Author
Siddhesh Jadhav

I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.