Set vs Frozenset in Python: Differences and Use Cases.
When working with data in Python, you might come across situations where you need to use sets or frozensets. Sets and frozensets are two data types that are commonly used in Python to store collections of unique elements. While both are similar in many ways, there are some important differences between them. In this blog post, we will explore the differences between sets and frozensets, and when you should use one over the other.
Set and frozenset are two built-in collection data types in Python that are used to store a collection of unique elements. While set is mutable, meaning that we can add, remove, or change elements in a set, frozenset is immutable and cannot be modified after creation.
Differences between set and frozenset
- Mutability: As mentioned earlier, set is mutable while frozenset is immutable. This means that we can add or remove elements from a set, but not from a frozenset.
- Memory usage: Since set is mutable, it requires more memory to store the elements as compared to frozenset, which is immutable and requires less memory.
- Hashability: A set can only contain hashable objects, i.e., objects that have a hash value that remains constant throughout their lifetime. On the other hand, frozenset can contain any immutable object, including other frozensets.
- Iteration: Both set and frozenset support iteration, but since frozenset is immutable, it can be iterated more efficiently than a set.
Use cases of set and frozenset
- Removing duplicates: One of the most common use cases of set is to remove duplicates from a list or any other sequence. Since sets only contain unique elements, we can convert the sequence to a set, which automatically removes the duplicates.
my_list = [1, 2, 3, 2, 1, 4, 5, 4]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3, 4, 5}
2. Membership testing: Both set and frozenset are efficient for membership testing. We can use the in
operator to check if an element is present in a set or a frozenset.
my_set = {1, 2, 3, 4, 5}
if 3 in my_set:
print("Element found")
else:
print("Element not found")
3. Storing hashable elements: Since sets only contain hashable elements, they are useful when we need to store a collection of unique hashable elements.
my_set = {'apple', 'banana', 'cherry'}
4. Hashing keys in a dictionary: Since frozenset is immutable, it can be used as a key in a dictionary, while set cannot be used as a key since it is mutable.
my_dict = {frozenset({1, 2}): 'value'}
print(my_dict) # Output: {frozenset({1, 2}): 'value'}
5. Memory usage: In terms of memory usage, frozenset
takes up less memory than a regular set
. This is because a set
is mutable, meaning that its elements can be added, removed, or changed after it is created. As a result, a set
needs to allocate extra memory to allow for these changes. On the other hand, a frozenset
is immutable, so it doesn't need this extra memory allocation, making it more memory-efficient.
To demonstrate this, you can use the sys.getsizeof()
function in Python to check the memory usage of objects.
import sys
s = {1, 2, 3, 4, 5}
fs = frozenset(s)
print(sys.getsizeof(s)) # output: 736
print(sys.getsizeof(fs)) # output: 224
To summarize, set
and frozenset
are both important data types in Python that offer unique features for handling collections of unique elements. set
is mutable and can be modified, while frozenset
is immutable and cannot be modified after creation. set
offers faster operations for modifications, while frozenset
offers better memory efficiency for read-only collections.
When choosing between set
and frozenset
, the decision should be based on the specific use case and requirements of the application. If you need a collection that can be modified or updated over time, then set
is the better choice. On the other hand, if you have a read-only collection that will not change, then frozenset
provides better memory efficiency and can be used as a hashable key for dictionaries and other data structures.