Home Forums Batch of Feb 26, 2024 frozenset

  • Posted by Debarpan on February 29, 2024 at 6:40 am

    what is the use case of frozenset as it is immutable once created.So we cant modify it.

    Deleted User replied 1 year ago 4 Members · 5 Replies
  • 5 Replies
  • Deleted User

    Member
    February 29, 2024 at 7:33 am

    In python frozenset is similar to set but unlike set, elements cannot be added or removed from a frozenset since they are frozen. Frozenset can be used as Dictionary keys(since keys in a Dictionary are immutable) and also as elements in another set.

    • POOJA BABAR

      Member
      February 29, 2024 at 2:56 pm

      example :

      fruits = {‘apple’, ‘banana’, ‘cherry’}
      x = frozenset(fruits) #
      x[1] = “strawberry” #Attempting to modify a frozenset will result in a TypeError

      # If you need to modify the set, we need to use a regular set instead

      fruits.add(“strawberry”)

      print(fruits)

      • Debarpan

        Member
        February 29, 2024 at 5:52 pm

        Yes I know about frozensets, I am just asking what is the usecase of it like for what purpose we can utilize it?

        • Deleted User

          Member
          February 29, 2024 at 6:08 pm

          Hi Debarpan. You can refer to my reply where I have mentioned two possible use cases of frozenset.

  • Neha

    Member
    February 29, 2024 at 3:24 pm

    Frozen set is just an immutable version of python set.

Log in to reply.

Get In Touch