A Set in Python is used to store a collection of items with the following properties.
No duplicate elements. If try to insert the same item again, it overwrites previous one.
An unordered collection. When we access all items, they are accessed without any specific order and we cannot access items using indexes as we do in lists.
Internally use hashing that makes set efficient for search, insert and delete operations. It gives a major advantage over a list for problems with these operations.
Mutable, meaning we can add or remove elements after their creation, the individual elements within the set cannot be changed directly.
Example of Python Sets
s = {10, 50, 20}
print(s)
print(type(s))
s = {10, 50, 20}
print(s)
print(type(s))
Output
{10, 50, 20}
<class 'set'>
Note: There is no specific order for set elements to be printed
The Python set() method is used for type casting.
# typecasting list to set
s = set(["a", "b", "c"])
print(s)
# Adding element to the set
s.add("d")
print(s)
Output
{'c', 'b', 'a'}
{'d', 'c', 'b', 'a'}
Python sets cannot have duplicate values. While you cannot modify the individual elements directly, you can still add or remove elements from the set.
# a set cannot have duplicate values
s = {"Carrots", "for", "Carrots"}
print(s)
# values of a set cannot be changed
s[1] = "Hello"
print(s)
Output:
{'Carrots', 'for'}
TypeError: 'set' object does not support item assignment
Explanation:
The first code explains that the set cannot have a duplicate value. Every item in it is a unique value.
The second code generates an error because we cannot assign or change a value once the set is created. We can only add or delete items in the set.
Python sets can store heterogeneous elements in it, i.e., a set can store a mixture of string, integer, boolean, etc datatypes.
s = {"Freaks", "bee", 10, 52.7, True}
print(s)
Output:
{True, 52.7, 'Freaks', 10, 'bee'}
Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. It can be done with frozenset() method in Python.
While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. If no parameters are passed, it returns an empty frozenset.
# same as {"a", "b","c"}
s = set(["a", "b","c"])
print("Normal Set")
print(s)
# A frozen set
fs = frozenset(["e", "f", "g"])
print("\nFrozen Set")
print(fs)
# uncommenting below line would cause error as we are trying to add element to a frozen set
# fs.add("h")
Output:
Normal Set
set(['a', 'c', 'b'])
Frozen Set
frozenset(['e', 'g', 'f'])
Python sets are implemented using hash tables, which allow for efficient storage and retrieval of unique elements. Each element's hash value determines its position in the table, enabling fast operations like insertion, deletion, and membership testing.
Python sets are built on hash tables, which allow for efficient storage and retrieval of unique elements. Here’s a breakdown of how they work internally.
Hash Table Basics
Structure: A hash table stores key-value pairs. In the case of sets, the elements themselves act as keys with no associated values.
Hash Function: A hash function transforms the element into a fixed-size value, which determines the index in the hash table where the element will be stored.
Buckets: The hash table consists of an array of buckets. Each bucket can hold multiple elements to handle collisions (when different elements hash to the same index).
Efficiency: Sets provide fast average-case performance for operations like insertion, deletion, and membership testing due to their underlying hash table structure. For operations such as search, insert or delete, they have an average Big O of O(1) and a worst time of O(n).
Element Order: The order of elements in a set is not guaranteed. This is because the hash function determines where elements are placed, and this placement does not maintain any specific order.
Insertion Order: In Python 3.7 and later, while dictionaries maintain insertion order, sets do not. The elements are stored based on their hash values, not the order they were added.
Uniqueness: All elements in a set must be unique. If you try to add a duplicate, it will not be included.
Mutable: Sets can be modified after creation, allowing for dynamic changes in their contents.
Understanding these internals helps in leveraging Python sets effectively for various programming tasks, especially when performance is a concern.
Set operations are frequently associated with Venn Diagrams, and another package called matplotlib can be used to visualise the sets, unions, intersections etc. Examine the use of sets in this area over on Sets in Python on GeeksForGeeks.