Sobes.tech
Junior — Middle

How is data stored inside a data structure in the form of a dictionary?

sobes.tech AI

Answer from AI

In Python, a dictionary (dict) stores data as key-value pairs. The dictionary is implemented using a hash table.

When a key-value pair is added, the key goes through a hash function that calculates an index in the array where the value will be stored. If a collision occurs (two keys with the same index), a collision resolution method is used (for example, chaining — linked list or open addressing).

This allows for fast (on average O(1)) lookup, addition, and deletion of elements by key.

Example:

my_dict = {'apple': 5, 'banana': 3}
print(my_dict['apple'])  # 5