![]() |
VOOZH | about |
Set is a built-in Python data type used to store a collection of unique items.
Example: This example shows how to create a set with some numbers and check its type.
{10, 50, 20}
<class 'set'>
Note: There is no specific order for set elements to be printed
set() method is used to convert other data types, such as lists or tuples, into sets.
{'c', 'b', 'a'}
{'d', 'c', 'b', 'a'}
Sets cannot have duplicate values. While you cannot modify the individual elements directly, you can still add or remove elements from the set.
Output
{'Geeks', 'for'}
TypeError: 'set' object does not support item assignment
Explanation:
Sets can store heterogeneous elements in it, i.e., a set can store a mixture of string, integer, boolean, etc datatypes.
{True, 52.7, 'Geeks', 10, 'for'}
Frozenset is an immutable version of a set. Its elements cannot be changed after creation, but you can perform operations like union, intersection and difference. Use frozenset() to create one.
('Normal Set:', set(['a', 'c', 'b']))
('Frozen Set:', frozenset(['e', 'g', 'f']))
Note: Frozensets are immutable, so methods like add() or remove() cannot be used. They are also hashable, which allows them to be used as dictionary keys.
Python sets are implemented using a hash table, similar to dictionaries, where the set elements are stored as keys with dummy values. If multiple elements map to the same index, Python handles the collision by storing them in the same bucket.
The diagram below illustrates how sets internally manage collisions to support efficient insertion, deletion, and lookup operations.
Sets with Numerous operations on a single HashTable:
The hash table stores elements based on their computed index values for example, values 20 and 30 are placed at index 5, while 40 goes to index 6 and 50 to index 8. When multiple values share the same index, they are linked together, forming a chain (as seen at index 5).
During traversal, insertion or deletion, the algorithm moves through these linked elements at each index to access or modify data efficiently.
1. Adding elements:add() function is used to insert new elements into a set. It automatically ignores duplicates.
{'c', 'd', 'a', 'b'}
2. Union:union() function combines two sets and returns a new set with all unique elements.
{'z', 'y', 'x'}
3. Intersection:intersection() function returns a new set containing elements that are common to both sets.
{2, 3}
4. Difference: difference() function returns a set containing elements that are in the first set but not in the second.
{1}
5. Clear: clear() function removes all elements from a set, leaving it empty.
set()
Sets and frozen sets support the following operators:
| Operators | Notes |
|---|---|
| key in s | containment check |
| key not in s | non-containment check |
| s1 == s2 | s1 is equivalent to s2 |
| s1 != s2 | s1 is not equivalent to s2 |
| s1 <= s2 | s1 is subset of s2 |
| s1 < s2 | s1 is proper subset of s2 |
| s1 >= s2 | s1 is superset of s2 |
| s1 > s2 | s1 is proper superset of s2 |
| s1 | s2 | the union of s1 and s2 |
| s1 & s2 | the intersection of s1 and s2 |
| s1 - s2 | the set of elements in s1 but not s2 |
| s1 ˆ s2 | the set of elements in precisely one of s1 or s2 |