Convert Set to List in Python

How to convert the set to a list in Python? By using the list() function you can easily convert the set to a list. Besides these, there are other ways as well. Let’s see different ways to convert a set to a list in Python.

The List is a one-dimensional data structure that holds multiple data type elements including duplicates, whereas the Python Sets won’t allow duplicate elements. If you try to add an existing element to a set, it won’t get added.

1. Quick Examples of converting set to list

Following are quick examples of how to convert a set to a list.


# Consider the set with some elements
myset=set({12,32,6,"sparkby","examples"})

# Method 1: Convert to list using list()
done=list(myset)
print(done)
print(type(done))

# Method 2: Convert to list manually
done=[]
for i in myset:
  done.append(i)

# Method 3: Convert to list by unpacking the set.
done=[*myset]

# Method 4:  Convert to list using map() function.
done=list(map(lambda x: x, myset))

# Method 5:  Consider the set with some elements
# Convert to list using sorted()
myset=set({12,32,6,78,90})
done=sorted(myset)

2. Convert Set to List using list()

The list() function in python is used to create a list, so if we pass a set object to it, it converts the input set to a list. After conversion, we can check the type of the object by using type() function.

2.1 Syntax


# Syntax
list(myset)

2.2 Example for converting set to list using list()

Let’s see a few examples of how to convert a set to a list using the list() function in Python.


# Create set
myset=set({12,32,6,"sparkby","examples"})
print(myset)

# Output:
# {32, 'sparkby', 6, 'examples', 12}

# Convert Set to list
done=list(myset)
print(done)

# Output: 
# [32, 'sparkby', 6, 'examples', 12]

Use type(done) to check the data type of the object.

3. Convert Set to List Manually in Python

In case you wanted to do it manually, you can use the for loop to iterate over a set and add each element to the list. With this approach, first, you need to initialize the empty list and add the element to the list within for loop by using the append() method.

3.1 Syntax


# Manually convert set to list.
done=[]
for iterator in myset:
  done.append(iterator)

3.2 Example for converting set to list manually


# Consider the set with some elements
myset=set({12,32,6,"sparkby","examples"})

# Convert to list
done=[]
for i in myset:
  done.append(i)

print(done)

# Output:
# [32, 'sparkby', 6, 'examples', 12]

Here, we are iterating all 5 elements and storing one by one in list and finally we are displaying the list (done).

4. Convert Set to List using sorted()

The sorted() function will return the elements in ascending order in a list, so we can utilize this method. This method can take the set as input and return a list.

4.1 Syntax


# Here, myset is the actual set.
sorted(myset)

4.2 Example of Converting set to list using sorted()


# Consider the set with some elements
myset=set({12,32,6,78,90})

print(myset)

# Convert to list using sorted()
done=sorted(myset)
print(done)

# Output:
# {32, 6, 90, 12, 78}
# [6, 12, 32, 78, 90]

All the elements from the set were returned in a list in sorted order.

5. Convert Set to List by Unpacking the set

We can unpack the set by using * operator. So we need to pass the set name inside the list-[] with unpack operator.

5.1 Syntax


# Here, myset is the actual set.
[*myset]

5.2 Example of Using Unpacking


# Consider the set with some elements
myset=set({12,32,6,78,90})

print(myset)

# Convert to list by unpacking the set.
done=[*myset]
print(done)

# Output:
# {32, 6, 90, 12, 78}
# [32, 6, 90, 12, 78]

6. Convert Set to List using map()

The map() takes a lambda expression which will iterate all the elements in the set. Now, these elements are stored in a list by passing map() inside a list().

6.1 Syntax


# Here, myset is the actual set.
list(map(lambda iterator: iterator, myset))

6.2 Example of using map()


# Consider the set with some elements
myset=set({12,32,6,78,90})

print(myset)

# Convert to list using map() function.
done=list(map(lambda x: x, myset))
print(done)

# Output:
# {32, 6, 90, 12, 78}
# [32, 6, 90, 12, 78]

7. Conclusion

In this article, you have learned different ways to convert a set to a list in Python. All the examples that we discussed here are straightforward and simple to use in your project.

Leave a Reply