Junior — Middle
What is the data storage structure used in a set?
sobes.tech AI
Answer from AI
In Python, a set is implemented based on a hash table. It is a data structure that stores unique elements and provides fast access, addition, and removal in amortized O(1) time. Each element is hashed, and its hash is used to determine its position in the table. Thanks to this, a set efficiently checks for the presence of an element and does not allow duplicates.
Example of using a set:
my_set = {1, 2, 3}
my_set.add(4) # Adding an element
print(2 in my_set) # Checking for the presence of an element, output: True