How to Convert Set to List in Python

Here are different ways to convert a set to a list in Python:

  1. Using list() function
  2. Using sorted() function
  3. Using list comprehension
  4. Using unpacking(*) operator
  5. 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

Visual Representation of Python Convert Set to List using list() function

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

Visual Representation of Using sorted() function

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

Visual Representation of Using list comprehension

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

Visual Representation of Using unpacking(*) operator

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'>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.