DEV Community

Seenivasan A
Seenivasan A

Posted on

Python-Sets

Set is used to store a collection of items with the following properties.

  • No duplicate elements. If you try to insert the same item again, it is ignored because sets store only unique values.
  • 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.
s = {10, 50, 20}
print(s)
print(type(s))
Enter fullscreen mode Exit fullscreen mode

Output
{10, 50, 20}

Method |Shortcut |Description

  • add() Adds an element to the set
  • clear() Removes all the elements from the set
  • copy() Returns a copy of the set
  • difference() - Returns a set containing the difference between two or more sets
  • difference_update() -= Removes the items in this set that are also included in another, specified set
  • discard() Remove the specified item
  • intersection() & Returns a set, that is the intersection of two other sets
  • intersection_update() &= Removes the items in this set that are not present in other, specified set(s)
  • isdisjoint() Returns True if NO items of this set is present in another set
  • issubset() <= Returns True if all items of this set is present in another set
  • < Returns True if all items of this set is present in another, larger set
  • issuperset() >= Returns True if all items of another set is present in this set
  • > Returns True if all items of another, smaller set is present in this set
  • pop() Removes an element from the set
  • remove() Removes the specified element
  • symmetric_difference() ^ Returns a set with the symmetric differences of two sets
  • symmetric_difference_update() ^= Inserts the symmetric differences from this set and another
  • union() | Return a set containing the union of sets
  • update() |= Update the set with the union of this set and others

Check unique and Immutable
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 = {"Geeks", "for", "Geeks"}
print(s)

# values of a set cannot be changed
s[1] = "Hello"
print(s)
Enter fullscreen mode Exit fullscreen mode

Output

{'Geeks', 'for'}
TypeError: 'set' object does not support item assignment

Python frozenset

  • frozenset is an immutable version of a set.
  • Like sets, it contains unique, unordered, unchangeable elements.
  • Unlike sets, elements cannot be added or removed from a frozenset.
a = frozenset([1, 2, 3, 4])
b = frozenset([3, 4, 5, 6])

c = a.copy()
print(c)

print(a.union(b))
print(a.intersection(b))
print(a.difference(b))
print(a.symmetric_difference(b))
Enter fullscreen mode Exit fullscreen mode

Output
frozenset({1, 2, 3, 4})
frozenset({1, 2, 3, 4, 5, 6})
frozenset({3, 4})
frozenset({1, 2})
frozenset({1, 2, 5, 6})

Method |Shortcut |Description

  • copy() Returns a shallow copy
  • difference() - Returns a new frozenset with the difference
  • intersection() & Returns a new frozenset with the intersection
  • isdisjoint() Returns True if there is NO intersection between two frozensets
  • issubset() <= / < Returns True if this frozenset is a (proper) subset of another
  • issuperset() >= / > Returns True if this frozenset is a (proper) superset of another
  • symmetric_difference() ^ Returns a new frozenset with the symmetric differences
  • union() | Returns a new frozenset containing the union

Internal working of Set

  • 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.

  • Each index in the hash table represents a possible hash value where elements are stored based on their computed hash key.
  • When two elements map to the same index, they are linked together using a linked list, forming a chain at that position.
  • This chaining mechanism helps efficiently handle collisions, allowing multiple elements to exist at the same hash index without overwriting each other.

References
https://www.geeksforgeeks.org/python/sets-in-python/
https://www.w3schools.com/python/python_sets.asp

Top comments (0)