Sets and Frozensets in Python (Part 1)

by:

Share:  

Ever wondered what are sets and frozensets data types in Python? (Part 1)



Set and Match...Python wins!

In Python, sets and frozensets are two unordered collections of immutable objects.

Sets

Sets are used quite extensively in Python development and especially for membership testing using the in operator.

The set data type in Python is mutable.

Sets can also be created using the curly braces notation as follows:

first_set = set() # creates a new set
type(first_set) # returns <class 'set'>

even_numbers = { 2, 4, 6, 8, 10 } # a set with 5 even numbers in it
odd_numbers = { 1, 3, 5, 7, 9 } # a set with 5 odd numbers in it

Sets can also be created using the set() keyword. The set.add() method allows an item to be added into the result set. The set.remove() method allows an item to be removed from the result set.

The in operator is used to demonstrate membership testing in Python.

Here's an example:

a = set() # creates a new set
a.add(1) # adds a new value to the 'a' set
a.add(2)
a.add(3)
a.add(4)
a.remove(4) # removes the value 4 from the 'a' set

b = set()
b.add(3)
b.add(4)
b.add(5)

4 in a # False
3 in b # True

People who have studied Mathematics before will remember typical operations one can perform on sets such as:

  • Intersection (using the & operator)
  • Union (using the | operator)
  • Difference (using the - operator)

Here's a few examples:

a & b # intersection => set([3])

a | b # union => set([1, 2, 3, 4, 5])

a - b # unique to a => set([1, 2])

b - a # unique to b => set([4, 5])

If you add duplicate values in a set, these values will be discarded by the result set.

For Example:

names = ["Mark", "Joe", "Jack", "Joe", "John"] # only one Joe will remain.

Frozensets

A frozenset is pretty much like a set with the exception that it is immutable in nature.

Let's repeat this: The frozenset data type in Python is immutable. Once items have been added to a set, the frozenset cannot be modified.

We use the frozenset() keyword to create a new frozenset instance.

Here's an example:

first_frozenset = frozenset()
type(first_frozenset) # returns <class 'frozenset'>
multiplier = frozenset([1, 10, 10, 100, 1000, 10000])

multiplier.add(1000000) # This is not allowed and will throw an AttributeError.
multiplier.remove(1000) # As above. Not allowed.

It's worthwile mentioning that intersection, union and difference operations work just fine on frozensets.

a = frozenset([1, 2, 3])
b = frozenset([3, 4, 5])

4 in a # False
3 in b # True

a & b # frozenset({3})
a | b # frozenset({1, 2, 3, 4, 5})
a - b # frozenset({1, 2})
b - a # frozenset({4, 5})

Frozensets can feel limited but they are awesome at performance and very effective for membership testing, intersection, union and difference operations.

Do you use sets and frozensets in Python or want to suggest an improvement to this article? Please share it with us by leaving a reply below.

More to come soon in Part 2. Stay tuned.


About the Author

Alvin Luchmun
Alvin Luchmun is a freelance developer, trainer and co-founder at and currently lives in London. Alvin is passionate about anything tech/biz related but is also a firm believer in agile clean code, he loves preaching about code to fellow humanoids!.

Leave a comment