Sobes.tech
Junior — Middle

Can you explain the concept of a dictionary in Python and its main features?

sobes.tech AI

Answer from AI

In Python, a dictionary (dict) is a mutable, unordered (in versions before 3.7) collection of key-value pairs. Keys must be hashable objects (e.g., strings, numbers, tuples), and values can be any objects.

Main features of dictionaries:

  • Fast access to values by key (amortized access time O(1)).
  • Keys are unique within the dictionary.
  • You can dynamically add, modify, and delete key-value pairs.

Example of creating and using a dictionary:

person = {'name': 'Ivan', 'age': 30, 'city': 'Moscow'}
print(person['name'])  # Outputs: Ivan

# Adding a new key
person['job'] = 'engineer'

# Modifying a value
person['age'] = 31

# Checking for a key
if 'city' in person:
    print('City is specified')
Can you explain the concept of a dictionary in Python… - sobes.tech