Python: How to convert a set to a list and vice-versa

Updated: February 12, 2024 By: Guest Contributor Post a comment

Introduction

Whether you’re a beginner or an experienced Python programmer, understanding how to convert between sets and lists is essential. Doing so enables you to leverage the unique benefits of each data structure in your programs.

Basics of Sets and Lists

In Python, lists and sets are two different types of collections. A list is an ordered sequence of items, allowing duplicate values. In contrast, a set is an unordered collection of unique items. Because of these differences, sometimes it’s necessary to convert a set to a list or vice versa, depending on the operations you need to perform.

Converting a Set to a List

The simplest way to convert a set to a list is to use the list() constructor.

my_set = {1, 2, 3, 4, 5}
my_list = list(my_set)
print(my_list)
# Output: [1, 2, 3, 4, 5]

This method preserves the items but does not guarantee the order in the resulting list because sets are unordered.

Converting a List to a Set

To convert a list to a set, use the set() constructor. This is helpful for removing duplicates from a list or for operations requiring unique elements.

my_list = [1, 2, 2, 3, 4]
my_set = set(my_list)
print(my_set)
# Output: {1, 2, 3, 4}

Advanced Conversions

Now that we’ve covered the basics, let’s explore more advanced ways to convert between sets and lists in Python.

Converting and Sorting

One common operation after converting a set to a list is to sort the list. Since sets are unordered, converting a set into a list and then sorting it brings order to the items.

my_set = {'banana', 'apple', 'orange'}
my_list = list(my_set)
my_list.sort()
print(my_list)
# Output: ['apple', 'banana', 'orange']

This code snippet converts a set of strings into a list and then sorts it alphabetically.

Utilizing Comprehensions

List and set comprehensions offer a more Pythonic way to convert and filter data during the conversion process.

# List to set, filtering even numbers
my_list = [1, 2, 2, 3, 4, 5, 6]
my_set = {x for x in my_list if x % 2 == 0}
print(my_set)
# Output: {2, 4, 6}

# Set to list, converting to string type
my_set = {1, 2, 3, 4, 5}
my_list = [str(x) for x in my_set]
print(my_list)
# Output: ['1', '2', '3', '4', '5']

These operations demonstrate how to filter or modify items while converting between sets and lists in a single, readable line of code.

Performing Mathematical Set Operations during Conversion

When working with sets, sometimes it’s useful to leverage mathematical set operations like union, intersection, difference, and symmetric difference as part of the conversion process. For example:

set1 = {1, 2, 3}
set2 = {2, 3, 4}
# Converting to a list after performing a set operation
union_list = list(set1 | set2)
print(union_list)
# Output: [1, 2, 3, 4]

This method allows for efficient data manipulation before conversion to a list, ensuring the final list contains precisely the elements you need.

Conclusion

Converting between sets and lists in Python is a fundamental skill that opens up various possibilities for working with collections. From basic conversions to advanced techniques that involve sorting, comprehensions, and mathematical set operations, Python offers a wealth of methods for efficiently manipulating data structures to suit your program’s needs. By mastering these techniques, you can write cleaner and more effective Python code.