09-21-2022, 10:33 PM
Python Set to Tuple | Tuple to Set | 3 Easy Ways
5/5 – (1 vote)
Problem Formulation: Given a Python set. How to convert it to a tuple? And how to convert the tuple back to a set?
There are three main ways:
I’ll also give you a bonus method 4 that shows you what to do to retain the ordering information when converting a tuple to a set—so keep reading!![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Method 1: Convert Set to Tuple with tuple()
To convert a set to a tuple, pass the set into the function. This is a built-in Python function, so you don’t need to import or install any library to use it. The return value is a new tuple from the values in the set.
Here’s an example where you convert the set to a tuple :
By the way, here’s an explainer video on this function:
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FViJ1E-52hq4%2Fhqdefault.jpg)
Method 2: Convert Tuple to Set with set()
To convert a tuple to a set, pass the tuple into the function. This is a built-in Python function, so you don’t need to import or install any library. The return value is a new set from the values in the tuple.
Here’s an example where you convert the tuple to a set :
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfZJsKQPlzRg%2Fhqdefault.jpg)
Problem: However, the conversion process from a tuple to set doesn’t always work because if you try to convert a tuple of mutable values, Python will raise the !
Read on to learn more about this problem—and how to resolve it easily:![[Image: 1f449.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png)
Method 3: Convert Tuple to Set with Set Comprehension
A Python set is an unordered collection of unique immutable elements. Each element must define explicitly or implicitly the dunder method, i.e., must be hashable.
If you attempt to convert a tuple of mutable elements (e.g., lists) to a set, Python will raise an error such as the:
In this case, you can use set comprehension to convert each inner tuple element to an immutable type. For example, the expression converts each inner list to a tuple. The result is a set of immutable tuples.
Here’s the solution to this problem in a minimal code example:
The final “bonus” section introduces another elegant way to retain the ordering information in a set:
![[Image: BonusbaadshahGIF.gif]](https://blog.finxter.com/wp-content/uploads/2022/09/BonusbaadshahGIF.gif)
Bonus Method 4: Enumerate Elements
Sometimes, you want to associate each set or tuple element with a specific numerical “index” value, i.e., a unique integer identifier. The method to the rescue!
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F6SifUNXKWNA%2Fhqdefault.jpg)
The result is the respective container data structure with (identifier, value) tuples:
Especially in the case where you convert a tuple to a set, this makes a lot of sense because you can retain the information on the ordering of elements that would be otherwise lost after converting to a set.
Thanks for reading the whole article, my friend! You can join us here (we have cheat sheets too):
5/5 – (1 vote)
Problem Formulation: Given a Python set. How to convert it to a tuple? And how to convert the tuple back to a set?There are three main ways:
- Method 1: To convert a Python set to a tuple, use the function.Code:
tuple(my_set)
- Method 2: To convert a Python tuple to a set, use the function.Code:
set(my_tuple)
- Method 3: To convert a Python tuple of mutable elements to a set, use the expression to avoid aCode:
set(tuple(x) for x in my_tuple).Code:TypeError
I’ll also give you a bonus method 4 that shows you what to do to retain the ordering information when converting a tuple to a set—so keep reading!
![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Method 1: Convert Set to Tuple with tuple()
To convert a set to a tuple, pass the set into the
Code:
tuple()
Here’s an example where you convert the set Code:
{1, 2, 3}Code:
(1, 2, 3)Code:
my_set = {1, 2, 3}
my_tuple = tuple(my_set)
print(my_tuple)
# (1, 2, 3)By the way, here’s an explainer video on this function:
Method 2: Convert Tuple to Set with set()
To convert a tuple to a set, pass the tuple into the
Code:
set()
Here’s an example where you convert the tuple Code:
(1, 2, 3)Code:
{1, 2, 3}Code:
my_tuple = (1, 2, 3)
my_set = set(my_tuple)
print(my_set)
# {1, 2, 3}
Problem: However, the conversion process from a tuple to set doesn’t always work because if you try to convert a tuple of mutable values, Python will raise the Code:
TypeError: unhashable typeRead on to learn more about this problem—and how to resolve it easily:
![[Image: 1f449.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png)
Method 3: Convert Tuple to Set with Set Comprehension
A Python set is an unordered collection of unique immutable elements. Each element must define explicitly or implicitly the Code:
__hash__()If you attempt to convert a tuple of mutable elements (e.g., lists) to a set, Python will raise an error such as the
Code:
TypeError: unhashable type: 'list'Code:
my_tuple = ([1, 2], [3, 4], [5, 6])
my_set = set(my_tuple)
# TypeError: unhashable type: 'list'In this case, you can use set comprehension to convert each inner tuple element to an immutable type. For example, the expression
Code:
set(tuple(x) for x in my_tuple)Here’s the solution to this problem in a minimal code example:
Code:
my_tuple = ([1, 2], [3, 4], [5, 6])
my_set = set(tuple(x) for x in my_tuple)
print(my_set)
# {(1, 2), (3, 4), (5, 6)}The final “bonus” section introduces another elegant way to retain the ordering information in a set:
![[Image: BonusbaadshahGIF.gif]](https://blog.finxter.com/wp-content/uploads/2022/09/BonusbaadshahGIF.gif)
Bonus Method 4: Enumerate Elements
Sometimes, you want to associate each set or tuple element with a specific numerical “index” value, i.e., a unique integer identifier. The
Code:
enumerate()- Use to convert a set to an enumerated tuple.Code:
tuple(enumerate(my_set))
- Use to convert a tuple to an enumerated set.Code:
set(enumerate(my_tuple))
The result is the respective container data structure with (identifier, value) tuples:
Code:
my_set = {'Alice', 'Bob', 'Carl'} my_tuple = tuple(enumerate(my_set))
print(my_tuple)
# ((0, 'Carl'), (1, 'Bob'), (2, 'Alice')) my_set = set(enumerate(('Alice', 'Bob', 'Carl')))
print(my_set)
# {(2, 'Carl'), (0, 'Alice'), (1, 'Bob')}Especially in the case where you convert a tuple to a set, this makes a lot of sense because you can retain the information on the ordering of elements that would be otherwise lost after converting to a set.
Thanks for reading the whole article, my friend! You can join us here (we have cheat sheets too):

