The easiest and most efficient way to convert a Python set to list is using the list() constructor. It takes an iterable (like a set) as its argument and returns a new list.
# Define a set with numbers number_set = {24, 18, 12, 6} # Print the original set and its data type print("Set:", number_set) print("Type before conversion:", type(number_set)) number_list = list(number_set) # Print the converted list and its data type print("List:", number_list) print("Type after conversion:", type(number_list)) # Set: {24, 18, 12, 6} # Type before conversion: <class 'set'> # List: [24, 18, 12, 6] # Type after conversion: <class 'list'>
After the conversion, we checked the type of an object using the type() function.
The list() constructor does not change the order; it preserves the set’s current order. Sets do not preserve insertion order.
Empty Set
What if the set is empty? Well, if the set is empty, the converted list will be empty, too.
# Defining a set empty_set = {} # Print the empty set and its data type print("Set:", empty_set) print("Type before conversion:", type(empty_set)) empty_list = list(empty_set) # Print the converted empty list and its data type print("List:", empty_list) print("Type after conversion:", type(empty_list)) # Set: {} # Type before conversion: <class 'dict'> # List: [] # Type after conversion: <class 'list'>
Handling Duplicates
Let’s say you have a list with duplicate elements, but duplicates will be removed permanently after converting to a set. If you convert back set to list, the list will have unique elements.
main_list = [19, 21, 21, 18, 19] print(main_list) # Output: [19, 21, 21, 18, 19] my_set = set(main_list) print(my_set) # Output: {18, 19, 21} derived_list = list(my_set) print(derived_list) # Output: [18, 19, 21]
Mixed Data Types
The conversion works seamlessly even if a set contains values of mixed data types.
set = {"Krunal", True, 19, 21.18} print(set) # Output: {True, 'Krunal', 19, 21.18} list = list(set) print(list) # Output: [True, 'Krunal', 19, 21.18] # Order may vary
Nested Structures
What if an original set consists of iterables like tuples or lists? How would conversion go? Well, if an iterable is hashable, it can be a part of a set element. Since a list is not hashable, it cannot be a part of a set element, but a tuple is hashable, so you can create a set of tuples and convert it to a list.
set_of_tuples = {(11, 21), (18, 19)} print(set_of_tuples) # Output: {(11, 21), (18, 19)} list_of_tuples = list(set_of_tuples) print(list_of_tuples) # Output: [(11, 21), (18, 19)] # Order may vary
Immutable Sets (frozenset)
Instead of a basic set, what if you are working with a frozenset? Can you convert a frozenset to a list? The answer is yes; you can still use the list() constructor for the conversion.
frozen = frozenset({11, 21.1, "Krunal"}) print(frozen) # Output: frozenset({11, 'Krunal', 21.1}) frozen_list = list(frozen) print(frozen_list) # Output: [11, 'Krunal', 21.1] # Your order may vary because the set is unordered.
Set with single element None
What if our set contains only a single element, “None?“ How will the conversion go? Let’s find out.
single = {None} print(single) # Output: {None} list = list(single) print(list) # Output: [None]
Here are four other ways to convert a set to a list:
- Using sorted() (For ordered list)
- Using list comprehension
- Using extend()
- Using unpacking(*) operator
Method 1: Using sorted()
The sorted() function can convert a set to a list, but it will return a new list with an ascending order. Use this approach for ordered list.
number_set = {24, 18, 12, 6} print("Set:", number_set) # Output: Set: {24, 18, 12, 6} print("Type before conversion:", type(number_set)) # Output: Type before conversion: <class 'set'> number_list = sorted(number_set) print("List:", number_list) # Output: List: [6, 12, 18, 24] print("Type after conversion:", type(number_list)) # Output: Type after conversion: <class 'list'> # Your Output order may vary
Method 2: Using list comprehension
In list comprehension, we will iterate over each set element and create a new list with all those elements. If you want to process each element while conversing, you can use this approach.
number_set = {24, 18, 12, 6} print("Set:", number_set) # Set: {24, 18, 12, 6} print("Type before conversion:", type(number_set)) # Type before conversion: <class 'set'> number_list = [item for item in number_set] print("List:", number_list) # List: [24, 18, 12, 6] print("Type after conversion:", type(number_list)) # Type after conversion: <class 'list'> # Output order may vary
Method 3: Using extend()
If you have a set and an empty list and want to populate that empty list with set elements, you can use the list.extend() method. It’s like initializing an empty list and extending it with set elements.
# Define a set number_set = {24, 18, 12, 6} print("Set:", number_set) # Set: {24, 18, 12, 6} print("Type before conversion:", type(number_set)) # Type before conversion: <class 'set'> # Initialize an empty list number_list = [] # Extend the list by adding all elements from the set number_list.extend(number_set) print("List:", number_list) # List: [24, 18, 12, 6] print("Type after conversion:", type(number_list)) # Type after conversion: <class 'list'> # Your output order may vary
Method 4: Using unpacking(*) operator
The unpacking operator * unpacks all the elements of a set into a new list.
number_set = {24, 18, 12, 6} print("Set:", number_set) # Set: {24, 18, 12, 6} print("Type before conversion:", type(number_set)) # Type before conversion: <class 'set'> number_list = [*number_set] print("List:", number_list) # List: [24, 18, 12, 6] print("Type after conversion:", type(number_list)) # Type after conversion: <class 'list'> # Output order may vary
That’s all!