The most optimal and fastest way to convert a tuple to a frozenset is to use the frozenset() constructor and pass the “tuple” to it, which will return a frozenset. Ensure the tuple contains only hashable elements (e.g., numbers, strings, tuples).
main_tuple = (11, 21, 19, 21, 18, 19) frozen_set = frozenset(main_tuple) print(frozen_set) # Output: frozenset({11, 18, 19, 21})
Like a set, frozenset does not allow duplicate elements, so if a tuple contains duplicates, it will remove them automatically, and order will not be preserved.
The time complexity for a simple conversion is O(n), but preprocessing (e.g., recursive conversion) adds overhead.
If your input tuple contains unhashable elements like a list, set, or dict, it will raise: TypeError: unhashable type: ‘list’.
main_tuple = ([1, 2, 3], [4, 5, 6], [7, 8, 9]) frozen_set = frozenset(main_tuple) print(frozen_set) # Output: TypeError: unhashable type: 'list'
Handling unhashable elements
To fix the TypeError: unhashable type: ‘list’ or handle the tuples with unhashable elements, preprocess the tuple and convert mutable elements to immutable equivalents.
Flatten simple mutable types
def convert_hashable(element): if isinstance(element, list): return tuple(element) elif isinstance(element, set): return frozenset(element) elif isinstance(element, dict): return frozenset(element.items()) else: return element main_tuple = (11, [21, 31], {"K": 21, "L": 19}) processed_tuple = tuple(convert_hashable(e) for e in main_tuple) f_set = frozenset(processed_tuple) print(f_set) # Output: frozenset({frozenset({('K', 21), ('L', 19)}), (21, 31), 11})
Based on the types, if-else conditions were executed. If an input is a list, it is converted to a tuple.
If an element is set, we convert it into a frozenset directly, and if the input is a dict, we use the dict.items() first to get the key: value pairs and then use the frozenset() method on it.
Nested structure
If your input tuple has deeply nested mutable elements, you must recursively convert all components into hashable types.
def deep_conversion(element): if isinstance(element, list) or isinstance(element, tuple): return tuple(deep_conversion(e) for e in element) elif isinstance(element, set): return frozenset(deep_conversion(e) for e in element) elif isinstance(element, dict): return frozenset((k, deep_conversion(v)) for k, v in element.items()) else: return element nested_tuple = (11, [21, [19, {48, 60}]], {"K": [21, 19]}) processed = deep_conversion(nested_tuple) # Convert the entire structure into a frozenset flatten_frozen_set = frozenset([processed]) print(flatten_frozen_set) # Output: frozenset({(11, (21, (19, frozenset({48, 60}))), frozenset({('K', (21, 19))}))})
Empty tuple
If the input is an empty tuple, the frozenset() constructor will return an empty frozenset.
empty_tuple = () # Convert the empty tuple to a frozenset frozen_empty_tuple = frozenset(empty_tuple) print(frozen_empty_tuple) # Output: frozenset(())
Single-element tuple
If a tuple contains only one element, it will return a frozenset with that element too.
main_tuple = ("Krunal",) # Convert the empty tuple to a frozenset converted_frozen_set = frozenset(main_tuple) print(converted_frozen_set) # Output: frozenset({'Krunal'})
That’s all!