Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Tuple to Frozenset in Python

  • 25 Mar, 2025
  • Com 0
Python Tuple to Frozenset

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).

Converting Tuple to Frozenset in Python

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.

Converting an empty tuple to 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.

converting a single-element tuple to frozenset

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!

Post Views: 19
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Find the Length of a List in Python
How to Create and Check an Empty Tuple in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend