Wednesday, September 18, 2024
HomePythonPython Set to Tuple | Tuple to Set

Python Set to Tuple | Tuple to Set


💬 Drawback Formulation: Given a Python set. Find out how to convert it to a tuple? And convert the tuple again to a set?

There are three important methods:

  • Methodology 1: To transform a Python set to a tuple, use the tuple(my_set) operate.
  • Methodology 2: To transform a Python tuple to a set, use the set(my_tuple) operate.
  • Methodology 3: To transform a Python tuple of mutable parts to a set, use the expression set(tuple(x) for x in my_tuple) to keep away from a TypeError.

I’ll additionally offer you a bonus technique 4 that exhibits you what to do to retain the ordering info when changing a tuple to a set—so maintain studying! 🙂

Methodology 1: Convert Set to Tuple with tuple()

To transform a set to a tuple, move the set into the tuple() operate. This can be a built-in Python operate, so that you don’t must import or set up any library to make use of it. The return worth is a brand new tuple from the values within the set.

👉 Right here’s an instance the place you exchange the set {1, 2, 3} to a tuple (1, 2, 3):

my_set = {1, 2, 3}
my_tuple = tuple(my_set)
print(my_tuple)
# (1, 2, 3)

By the best way, right here’s an explainer video on this operate:

Methodology 2: Convert Tuple to Set with set()

To transform a tuple to a set, move the tuple into the set() operate. This can be a built-in Python operate, so that you don’t must import or set up any library. The return worth is a brand new set from the values within the tuple.

👉 Right here’s an instance the place you exchange the tuple (1, 2, 3) to a set {1, 2, 3}:

my_tuple = (1, 2, 3)
my_set = set(my_tuple)
print(my_set)
# {1, 2, 3}

Drawback: Nevertheless, the conversion course of from a tuple to set doesn’t at all times work as a result of when you attempt to convert a tuple of mutable values, Python will increase the TypeError: unhashable kind!

Learn on to study extra about this downside—and resolve it simply: 👉

Methodology 3: Convert Tuple to Set with Set Comprehension

💡 A Python set is an unordered assortment of distinctive immutable parts. Every factor should outline explicitly or implicitly the __hash__() dunder technique, i.e., have to be hashable.

In the event you try to convert a tuple of mutable parts (e.g., lists) to a set, Python will increase an error such because the TypeError: unhashable kind: 'listing':

my_tuple = ([1, 2], [3, 4], [5, 6])
my_set = set(my_tuple)
# TypeError: unhashable kind: 'listing'

On this case, you should use set comprehension to transform every inside tuple factor to an immutable kind. For instance, the expression set(tuple(x) for x in my_tuple) converts every inside listing to a tuple. The result’s a set of immutable tuples.

Right here’s the answer to this downside in a minimal code instance:

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 ultimate “bonus” part introduces one other elegant approach to retain the ordering info in a set:

Bonus Methodology 4: Enumerate Parts

Generally, you need to affiliate every set or tuple factor with a particular numerical “index” worth, i.e., a novel integer identifier. The enumerate() technique to the rescue!

  • Use tuple(enumerate(my_set)) to transform a set to an enumerated tuple.
  • Use set(enumerate(my_tuple)) to transform a tuple to an enumerated set.

The result’s the respective container knowledge construction with (identifier, worth) tuples:

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

Particularly within the case the place you exchange a tuple to a set, this makes a number of sense as a result of you possibly can retain the knowledge on the ordering of parts that may be in any other case misplaced after changing to a set.


Thanks for studying the entire article, my buddy! You may be a part of us right here (we’ve cheat sheets too):


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments