![]() |
|
[Tut] Python Set to Tuple | Tuple to Set | 3 Easy Ways - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Set to Tuple | Tuple to Set | 3 Easy Ways (/thread-99969.html) |
[Tut] Python Set to Tuple | Tuple to Set | 3 Easy Ways - xSicKxBot - 09-21-2022 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! ![]() 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: ![]() 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: ![]() 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()
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): |