Use the tuple() constructor to convert a set into a tuple and use the set() constructor to convert a tuple to a set in Python.
For creating an immutable, ordered sequence, we need a tuple.
To remove duplicates from our sequence or enable fast membership testing, we need a set.
Both data structures have their unique advantages that are helpful, and we can take advantage of them based on our specific requirements. That’s why conversion from one to another is necessary.
Converting a Set to a Tuple
The tuple() constructor accepts a sequence, such as a set, and creates a tuple out of it.
input_set = {10, 20, 30, 40} print(type(input_set)) # Output: <class 'set'> output_tuple = tuple(input_set) print(output_tuple) # Output: (40, 10, 20, 30) print(type(output_tuple)) # Output: <class 'tuple'>
Using the type() function, we can check the variable’s data type. By printing before and after the operation, we can verify that we indeed have a tuple in the output.
Since the set is an unordered collection, the output tuple is also unordered.
Empty Set
The result of converting an empty set is just an empty tuple.
empty_set = set() empty_tuple = tuple(empty_set) print(empty_tuple) # Output: () print(type(empty_tuple)) # Output: <class 'tuple'>
Mixed types
As long as it is hashable, the set can contain mixed data type values.
After conversion to a tuple, it also preserves the types, but the order is arbitrary.
mixed_type_set = {11, "Eleven", 6.626} mixed_type_tuple = tuple(mixed_type_set) print(mixed_type_tuple) # Output: (11, 'Eleven', 6.626) print(type(mixed_type_tuple)) # Output: <class 'tuple'>
Hashability for dictionary keys
What if you want your tuple to serve as a dictionary key, but you currently have a set? Well, that’s where you need to convert it to a tuple because tuples are hashable, but sets are not.
input_set = {"apple", "Nvidia"} # Converting to hashable tuple dict_key = tuple(input_set) output_dict = {dict_key: 500} print(output_dict) # Output: {('apple', 'Nvidia'): 500}
The above output shows that we have a tuple with two elements, a key and 500 is its value.
Sorted conversion
To get the sorted tuple after the conversion, we can use the built-in sorted() method. First, sort the set and then convert it to a tuple.
input_set = {30, 11, 19, 21} sorted_tuple = tuple(sorted(input_set)) print(sorted_tuple) # Output: (11, 19, 21, 30)
Converting a Tuple to a Set
The built-in set() constructor accepts a tuple as an argument and creates a set from it, removing duplicates if they exist.
input_tuple = (19, 21, 21, 11) print(type(input_tuple)) # Output: <class 'tuple'> output_set = set(input_tuple) print(output_set) # Output: {11, 19, 21} print(type(output_set)) # Output: <class 'set'>
As you can see, the 21st element has a duplicate, which is removed from output_set, resulting in a final set with only three elements, and it is ordered.
Mixed types
When the input tuple has mixed-type elements, the output set will have the same without any error. Both can contain different types of elements.
mixed_tuple = (10, "Ben", True, 19.21) print(type(mixed_tuple)) # Output: <class 'tuple'> output_mixed_set = set(mixed_tuple) print(output_mixed_set) # Output: {True, 10, 19.21, 'Ben'} print(type(output_mixed_set)) # Output: <class 'set'>
Tuple with unhashable elements
A tuple cannot contain unhashable elements like a list, and if you attempt, it will throw TypeError: unhashable type: ‘list’ error.
try: invalid_tuple = (1, [2, 3]) # List inside tuple invalid_set = set(invalid_tuple) except TypeError as e: print(e) # Output: unhashable type: 'list'
Empty Tuple
The outcome of transforming an empty tuple is an empty set.
empty_tuple = () empty_set = set(empty_tuple) print(empty_set) # Output: set()
That’s all!