Sobes.tech
Junior — Middle

Can you list the key features of the dict data structure in Python and how it differs from other collections?

sobes.tech AI

Answer from AI

In Python, a dict is a built-in data structure that implements a mapping (associative array), where each key is associated with a value.

Key features of dict:

  • Unique keys: each key in the dictionary is unique.
  • Fast access: search, insert, and delete operations by key are performed on average in O(1) time thanks to hashing.
  • Unordered (before Python 3.6): before version 3.6, the order of elements was not guaranteed; from 3.7 onwards, insertion order is preserved.
  • Mutable: the dictionary can be modified — adding, removing, and updating key-value pairs.
  • Keys must be hashable: usually immutable types (strings, numbers, tuples).

Differences from other collections:

  • Unlike lists (list), which are accessed by index, dict is accessed by key.
  • Unlike sets (set), which store only unique elements without values, dict stores key-value pairs.
  • Unlike tuples and lists, dict is not indexed by numeric positions but by keys of arbitrary types.

Example usage:

person = {'name': 'Ivan', 'age': 30}
print(person['name'])  # Ivan
person['age'] = 31  # update value
person['city'] = 'Moscow'  # add new pair
Can you list the key features of the dict data… - sobes.tech