Junior — Middle
What is the role and application of a dictionary in Python?
sobes.tech AI
Answer from AI
In Python, a dictionary (dict) is a mutable data structure that stores key-value pairs. It allows quick access to data by a unique key, which is convenient for organizing and searching for information.
Applications of dictionaries:
- Storing settings or program parameters.
- Representing objects with attributes.
- Counting the frequency of elements (e.g., words in a text).
- Fast search and update of data by key.
Example of usage:
user = {'name': 'Ivan', 'age': 30, 'city': 'Moscow'}
print(user['name']) # Outputs: Ivan
user['age'] = 31 # Updating the value
Dictionaries are convenient because keys can be strings, numbers, or other immutable types, and access to values is very fast.