How to Change Set to List in Python

Sets and lists are two important elements of the Python programming language. At first glance, they might even seem identical to each other. We assign both sets and lists in a similar manner.

Sets and lists can also contain similar data types. We can contain a string or integer within each. We even access components from sets and lists in a similar manner. And unlike a Python dictionary that contains key value pairs, the set and list comprehension Python data structure use singular values. However, we’ll see some of their significant differences as we examine methods to convert sets to lists in Python. We can begin with a simple example that uses the python list function.

ourSet = {1,2,3,4,5,6,7,8,9,10,10}
ourList = list(ourSet)
print(ourList)

This demonstrates one of the simplest set to list Python methods. The magic comes from the fact that we’re able to use the Python list function for the conversion. We begin by creating our initial Python set. Note how similar this is to the methods we’d use to declare a list or python tuple data type. It might not appear that there’s any difference other than the fact that we use {} to contain data rather than []. After all, these are both a type of Python data structure sequence. However, as we go through the code you should note the fact that we have two instances of 10 within our set comprehension, so it is not a unique element.

In the next line we declare a variable, ourList, from the returned value of the list function. Finally, we print ourList to screen in the final line. But recall that we declared two instances of the number ten when creating ourSet. Why is ourList only showing one instance of the number ten? This isn’t a problem with the conversion method. It instead demonstrates one of the differences between sets and lists.

A set object can’t contain duplicate values while a list item can. Python won’t exit with an error if we declare a set with duplicate elements. However, the end result will only contain unique valuefs. So when we convert the set to list we still only have one instance of the index number ten as a list element.

Using the convert list function is one of the fastest and easiest ways to convert every single item from a set to a list. But this is far from the only way to go about the task. Consider a scenario where we wanted to have a little more control over the process. The following Python code will manually construct a list from a set.

ourSet = {1,2,3,4,5,6,7,8,9,10}
ourList = []
for x in ourSet:     
   ourList.append(x)
print(ourList)

We begin by once again creating a set of numbers. Next, we make an empty list to populate with the converted set unique values or even duplicate items. We take advantage of the fact that we’re dealing with an iterable object and work our way through the contents of ourSet for each individual element. With each iteration we append a value from ourSet into ourList. Finally, we can print out ourList to verify that we have indeed converted the set into a list. However, there’s still more that we could add to the conversion.

If you’ve used lists then you’re probably quite familiar with their versatility as a container. Lists can, in fact, hold sets. A list object can even contain multiple sets in a similar fashion to a nested list. The following code shows how we can convert a list of sets into a single flat list. We could use nested loops for this conversion. But we’ll instead use a combination of functions that can vastly simplify the process.

setList = [{1, 2, 3}, {4, 5, 6}, {7, 8, 9},{10}]
ourList = list(set().union(*setList))
print(ourList)

We begin by creating our list of sets and assigning it to setList. Next, we’ll create a new list object called ourList. We populate ourList by using the list function. However, we first need to use set’s union functionality on the contents of setList. Union melds all of the sets within setList to a singular set. This set is passed to list. And the results are stored in ourList. Finally, we can print the contents of ourList to screen just as we would with any other list.

How to Change Set to List in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top