← Back to Python Course | Chapter 6: Data Structures | Lesson 11 of 12

Python Frozenset

एक normal Python set बनने के बाद भी modify किया जा सकता है — items स्वतंत्र रूप से add या remove हो सकते हैं — पर कभी-कभी आपको एक ऐसा set-like collection चाहिए होता है जो कभी बदले न, जैसे उसे dictionary key के रूप में इस्तेमाल करते समय (जिसके लिए एक immutable, hashable value चाहिए) या बस data को गलती से बदलने से बचाने के लिए। frozenset() ठीक यही बनाता है: set का एक immutable version।
Syntax
python
frozen = frozenset(iterable)

Frozenset बनाना

frozenset(iterable) किसी भी iterable — list, tuple, regular set, या characters की string — से एक immutable set बनाता है, जो पढ़ने के लिए set जैसा ही व्यवहार करता है, पर बनते ही अपनी contents को permanently lock कर देता है।

Note: किसी मौजूदा set, list, या tuple को सीधे frozenset() में पास करके उसका एक immutable snapshot बनाइए, बिना original को बदले।
Warning: बिना किसी arguments के frozenset() एक खाली frozenset बनाता है, पर इसके लिए कोई literal syntax shortcut नहीं है, उसी तरह जैसे {} खाली dictionary और खाली set के बीच ambiguous होता।

उदाहरण: Creating a Frozenset

python
numbers = frozenset([1, 2, 2, 3])
print(numbers)

Frozensets Immutable होते हैं

एक बार बनने के बाद, frozenset किसी भी तरह से बदला नहीं जा सकता — इसमें add(), remove(), discard(), pop(), या clear() जैसा कोई method उपलब्ध नहीं है, क्योंकि ये सब इसकी contents को modify कर देते, जो frozenset के पूरे मकसद के सीधे खिलाफ़ है।

Note: अगर आपको लगे कि किसी frozenset की contents modify करनी पड़ रही हैं, तो यह एक strong signal है कि आपको असल में एक regular, mutable set चाहिए।
Warning: किसी frozenset पर mutating method (जैसे .add()) call करने पर तुरंत AttributeError raise होता है, क्योंकि वह method frozenset type पर मौजूद ही नहीं है।

उदाहरण: Frozensets Are Immutable

python
numbers = frozenset([1, 2, 3])
try:
    numbers.add(4)  # frozenset has no add() method
except AttributeError as e:
    print("Error:", e)

Frozensets को Dictionary Keys के रूप में

चूँकि frozenset immutable है और इसलिए hashable भी है, इसे dictionary key के रूप में इस्तेमाल किया जा सकता है या किसी दूसरे set के अंदर element के रूप में रखा जा सकता है — ये दोनों operations एक regular, mutable set के साथ TypeError raise करते हैं, क्योंकि Python को dictionary keys और set elements कभी न बदलने चाहिए ऐसा चाहिए होता है।

Note: frozenset तब लीजिए जब आपको specifically एक set-like value चाहिए जो dictionary key बने या किसी दूसरे set के अंदर रहे।
Warning: किसी regular set (frozenset नहीं) को dictionary key के रूप में इस्तेमाल करने की कोशिश करने पर तुरंत "TypeError: unhashable type: set" आता है।

उदाहरण: Frozensets as Dictionary Keys

python
key = frozenset([1, 2])  # frozensets are hashable, so they can be dict keys
data = {key: "pair one-two"}
print(data[frozenset([1, 2])])  # an equal frozenset looks up the same entry

Frozensets पर Set Operations

Frozensets बिल्कुल वही read-only set operations support करते हैं जो regular sets करते हैं — union (|), intersection (&), difference (-), और membership testing (in) — क्योंकि इनमें से कोई भी operation frozenset को खुद modify नहीं करता, बस उसे किसी और चीज़ के साथ मिलाकर एक नया result बनाता है।

Note: किसी भी read-only operation (membership checks, unions, intersections) के लिए frozensets को बिल्कुल regular sets जैसे इस्तेमाल कीजिए — immutability सिर्फ modification को रोकती है, पढ़ने या combine करने को नहीं।
Warning: दो frozensets के बीच union या intersection एक नया frozenset बनाता है, regular set नहीं — immutability इन combining operations के through भी आगे बढ़ती है।

उदाहरण: Set Operations on Frozensets

python
a = frozenset([1, 2, 3])
b = frozenset([2, 3, 4])
print(a | b)  # union, produces a new frozenset
print(a & b)  # intersection, also produces a new frozenset

frozenset बनाम set कब इस्तेमाल करें

एक regular set तब चुनिए जब collection को program के जीवन-काल में genuinely बढ़ना या सिकुड़ना हो; frozenset specifically तब चुनिए जब आपको एक immutable, hashable set-like value चाहिए — constants के किसी fixed collection को गलती से बदलने से बचाने के लिए, या dictionary key या set element के रूप में इस्तेमाल करने के लिए।

Note: जब तक कोई specific reason (immutability, hashability) न हो, frozenset की बजाय regular set को default मानिए।
Warning: सावधानी के नाम पर हर जगह frozenset इस्तेमाल करना, यहाँ तक कि उन collections के लिए भी जिन्हें genuinely समय के साथ बदलना है, simple in-place updates की बजाय हर बार "नया frozenset बनाओ" जैसे awkward workarounds मजबूर करता है।

उदाहरण: When to Use frozenset vs set

python
mutable_set = {1, 2, 3}
mutable_set.add(4)  # regular set can grow

immutable_set = frozenset([1, 2, 3])  # frozenset can't be changed after creation
print(mutable_set, immutable_set)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.