DEV Community

Rahul Banerjee
Rahul Banerjee

Posted on • Originally published at

#Day4 - Sets and Frozensets in Python

Sets and Frozen Sets are built-in data types in Python. Both of them are unordered datatypes used to store distinct elements. If you have worked with a different programming language, you might be familiar with Set. However, Frozen Sets are not so common and unique to Python. The biggest advantage of a set and frozen set are the fact that they have a lookup time of O(1).

Frozen Sets are built on top of Sets and as a result, they have many similarities.

A quick summary of their similarities and differences, all of them are discussed.

  • Both contain distinct elements only
  • Traversing
  • Lookup
  • Both can contain Hashable elements only
  • Intersection, Union, and Difference


  • Creation
  • A Frozen set is hashable and mutable while a Set is not hashable and immutable
  • A Frozen set can be used as a key in a dictionary


To create a set or a frozen set, all we need is iterable. It will only store the distinct elements.


Treversing a Set/Frozen Set

Both support **** and can be traversed.



Both sets and frozen sets can only store hashable elements. You can not create a set of lists since a list is not hashable. Since sets and frozen sets only contain hashable elements, each element has a hash value associated with it. As a result, using the in operator takes O(1) time. Since they are unordered, both of them do not support indexing


The picture was taken from this article on Medium

Intersection, Union and Difference in Sets/Frozen Sets

  • Intersection : & (and)
  • Union : | (pipe)
  • Difference: - (subtraction)



A set is mutable while a frozen set is not. This basically means that a set can be updated after it's created but a frozen set can not. Once a frozen set is created, the elements inside it are frozen.



A frozen set is hashable, i.e it can have a hash value. On the other hand, a set is not hashable. As a result of this, a frozen set can be used as a key for a dictionary but a set can not be used.



  • Both Frozen Sets and Sets have O(1) lookup time
  • Neither of them supports indexing
  • Both can be traversed using
  • A set can be updated after creation but a frozen set can not be updated
  • Only Frozen Sets are hashable and can be used as dictionary keys

Top comments (0)