Converting Set to List in Python
Converting ‘set’ datatype to ‘list’ datatype is called typecasting.
Typecasting in programming is a method to convert one datatype into another datatype. It may happen implicitly by the defined language, called implicit typecasting. It may be achieved by using some methods or functions, called explicit typecasting.
During python programming or programming in general, typecasting datatype is a useful skill to manipulate data.
We will look at how to typecast set to list in python3 in this article.
Approach1: Using list(set_name)
Code:
# Converting set to list
# In python programming
set1 = {‘java’, ‘python’, ‘c++’}
list1 = list(set1)
print(set1)
print(list1)
Output:
{‘java’, ‘python’, ‘c++’}
[‘java’, ‘python’, ‘c++’]
In the above approach, we created a set named “set1” with homogenous variables. Then we used typecasting by using list(set1). As we can see from the output, the set data type is converted to a list.
If a set of elements are of a similar datatype it is called a homogeneous element set.
If a set of elements are of a different datatype it is called a heterogeneous element set.
Let us see, how to convert a set to a list if the set has heterogeneous variables in it.
Code:
# Converting set to list
# In python programming
set1 = {‘java’, ‘python’, ‘1.02’, ‘none’}
list1 = list(set1)
print(set1)
print(list1)
Output:
{‘java’, ‘python’, ‘1.02’, ‘none’}
[‘java’, ‘python’, ‘1.02’, ‘none’]
Approach2: Using Manual Iteration
Another easy-to-use approach is to use the ‘for’ loop(manual iteration). Then, append each element to an empty list.
Code:
# Converting set to list
# In python programming
#using manual iteration
set1 = {‘java’, ‘python’, ’12.0’, ‘none’}
list1 = []
for i in set1:
list1.append(i)
print(set1)
print(list1)
Output:
{‘java’, ‘python’, ’12.0’, ‘none’}
[‘java’, ‘python’, ’12.0’, ‘none’]
Approach3: Using user-defined function
This approach of using a user-defined function is generally used if we need to do a certain process multiple times throughout the code. Like, in this case, if I have to convert the set to a list more than once in the entire code, I will choose a user-defined function. It also increases the readability of the code.
Code:
# Converting set to list
# In python programming
#using user-defined function
def convert(set):
return list(set)
set1 = {‘java’, ‘python’, ’12.0’, ‘none’}
list1 = convert(set1)
print(set1)
print(list1)
Output:
{‘java’, ‘python’, ’12.0’, ‘none’}
[‘java’, ‘python’, ’12.0’, ‘none’]
Approach4: Using frozenset()
A frozen set object is an immutable unordered collection of data elements in a set. It can be modified for the entirety of its code life. To convert a frozenset into a list pass the set as a parameter to the list. Let’s write a code to understand the same.
Code:
# Converting set to list
# In python programming
#using frozenset
set1 = frozenset({‘j’, ‘p’, ’12.0’, ‘none’, (1, 2, 3)})
list1 = list(set1)
print(set1)
print(list1)
Output:
{‘j’, ‘p’, ’12.0’, ‘none’, (1, 2, 3)}
[‘j’, ‘p’, ’12.0’, ‘none’, (1, 2, 3)]
Approach5: Using keyword arguments to unpack set in a list
Each element of the set can be passed as a keyword argument into a list. Each element is separated by a comma. They will be stored in the list as an individual elements and not as a set. It is easy to use and fast method for converting set to list. Due to a decrease in the readability of the code, it is not highly used. Let’s look at a sample code.
Code:
# Converting set to list
# In python programming
#using *kwrg
sample_set = {10, 20, 30, 40}
my_list = [*sample_set, ]
print(sample_set)
print(my_list)
Output:
{10, 20, 30, 40}
[10, 20, 30, 40]
Above mentioned approaches are a few of many that exist in common practice. Few other modules and approaches may be explored for faster and cleaner coding.
Frequently asked questions (FAQs) related to converting set to list
Q) Why a set cannot be an element of another set while a list can have another list as an element?
A) Set object in python cannot store any element that doesn’t have a consistent hashing value. Since sets are immutable but also unordered they are unhashable objects. That means that do not generate consistent value on hashing.
Q) Why does converting a list into a set throw an error “Unhashable object”?
A) Since the list is a mutable type object in python, it does not produce consistent hash values. It is an unhashable type and thus, cannot be converted into a set. But, if we have a simple list of homogenous elements, it produces a consistent hash value. Therefore, it can be converted to set in python.