Here are different ways to convert a set to a list in Python:
- Using list() function
- Using sorted() function
- Using list comprehension
- Using unpacking(*) operator
- Using extend() method
Method 1: Using list() function
The easiest way is to use the list() function, which takes an iterable (like a set) as its argument and returns a new list.
Visual Representation
Example
# 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))
Output
Set: {24, 18, 12, 6}
Type before conversion: <class 'set'>
List: [24, 18, 12, 6]
Type after conversion: <class 'list'>
Method 2: Using sorted() function
If you want the list to be sorted, you can use the sorted() function, which returns a new list in ascending order.
Visual Representation
Example
number_set = {24, 18, 12, 6}
print("Set:", number_set)
print("Type before conversion:", type(number_set))
number_list = sorted(number_set)
print("List:", number_list)
print("Type after conversion:", type(number_list))
Output
Set: {24, 18, 12, 6}
Type before conversion: <class 'set'>
List: [6, 12, 18, 24]
Type after conversion: <class 'list'>
Method 3: Using list comprehension
List comprehension iterates over each element in a set and creates a new list.
Visual Representation
Example
number_set = {24, 18, 12, 6}
print("Set:", number_set)
print("Type before conversion:", type(number_set))
number_list = [item for item in number_set]
print("List:", number_list)
print("Type after conversion:", type(number_list))
Output
Set: {24, 18, 12, 6}
Type before conversion: <class 'set'>
List: [24, 18, 12, 6]
Type after conversion: <class 'list'>
Method 4: Using unpacking(*) operator
The unpacking operator * unpacks all the elements of a set into a new list.
Visual Representation
Example
number_set = {24, 18, 12, 6}
print("Set:", number_set)
print("Type before conversion:", type(number_set))
number_list = [*number_set]
print("List:", number_list)
print("Type after conversion:", type(number_list))
Output
Set: {24, 18, 12, 6}
Type before conversion: <class 'set'>
List: [24, 18, 12, 6]
Type after conversion: <class 'list'>
Method 5: Using extend() method
If you already have a list and want to add elements from a set to it, you can use the list.extend() method.
Example
# Define a set with numbers
number_set = {24, 18, 12, 6}
print("Set:", number_set)
print("Type before conversion:", type(number_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)
print("Type after conversion:", type(number_list))
Output
Set: {24, 18, 12, 6}
Type before conversion: <class 'set'>
List: [24, 18, 12, 6]
Type after conversion: <class 'list'>
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.