I'm learning Python 3 using The Quick Python Book, where the author talks about frozensets, stating that since sets are mutable and hence unhashable, thereby becoming unfit for being dictionary keys, their frozen counterparts were introduced. Other than the obvious difference that a tuple is an ordered data structure while frozenset, or more generally a set, is unordered, are there any other differences between a tuple and a frozenset?


tuples are immutable lists, frozensets are immutable sets.

tuples are indeed an ordered collection of objects, but they can contain duplicates and unhashable objects, and have slice functionality

frozensets aren't indexed, but you have the functionality of sets - O(1) element lookups, and functionality such as unions and intersections. They also can't contain duplicates, like their mutable counterparts.

  • 1
    So frozensets give me duplicate removal, set operations and the lookup only takes constant time, nice :) – legends2k Jan 20 '13 at 7:09
  • @legends2k well they are still sets ;) – Volatility Jan 20 '13 at 7:11
  • Yeah, I see that all the differences between tuples and frozensets are really differences between tuples and sets. – legends2k Jan 20 '13 at 7:13
  • 1
    @Volatility Also there's another parallel: tuples and frozensets are hashable; lists and sets aren't. – Elliot Cameron Jul 11 '16 at 21:38
  • 1
    @AndreaBergonzo tuples that contain unhashable objects are actually unhashable themselves (e.g. try putting ([],) in a set). They will be hashable otherwise, though, unlike lists which are always unhashable. – Volatility Dec 6 '17 at 7:39

Somewhat counter intuitive - what about this bon mot:

sss = frozenset('abc')
sss |= set('efg')

Will yield:

frozenset(['a', 'c', 'b', 'e', 'g', 'f'])

Of course, this is equivalent to x = x | y, so not changing the original frozenset, but it doesn't half make a mockery of the term 'immutable' to the code reviewer!

  • if you put: sss = set('abc'). It will do the same – Ben Oct 26 '15 at 2:59
  • @Ben, I think you missed the intent - frozenset is immutable, so starting and ending with different frozenset contents in x might catch out some code reviewers. Sets are expected to change, so you read the code differently. – F1Rumors Oct 26 '15 at 17:18
  • 6
    Immutable doesn't mean you can't reuse the variable name! If you run id(sss), you'll see that the first sss variable represents a different object than the second sss variable. (immutable str objects work the same way.) – dbn Apr 8 '16 at 16:32

One difference that comes to mind is the issue of duplicates. A tuple of (1, 1, 1, 1, 2, 2, 2) would be exactly what you expect, but a frozenset would remove all of those duplicates, leaving you with frozenset([1, 2]).


Volatility does mention that frozensets are not indexed. I was looking at the other functionality, so did not immediately realize that standard python slicing is not possible.

a = frozenset((1, 1, 1, 1, 2, 2, 2))  # results in frozenset([1, 2])
print a[0] 

will give error:

TypeError: 'frozenset' object does not support indexing

Obvious from fact that it is not indexed, but though it was worth adding explicitly here

  • 1
    Yeah, it follows from the fact that frozensets are also sets and thus don't support indexing like sets. – legends2k May 27 '13 at 11:25
  • Sets are not ordered, so what does it mean? If you are just wanting an item from it, you can remember that they are iterable and do something like print iter(a).next() ... – F1Rumors Jul 19 '17 at 18:49

Your Answer

