Junior — Middle
Can you explain what mutable data structures are and how they work?
sobes.tech AI
Answer from AI
Mutable data structures are objects whose state can be changed after creation. In Python, examples of such structures include lists (list), dictionaries (dict), and sets (set).
When you modify a mutable object, you change its contents rather than creating a new object. This is convenient for operations that require frequent data updates without creating copies.
Example:
my_list = [1, 2, 3]
my_list.append(4) # Modifies the original list
print(my_list) # Outputs: [1, 2, 3, 4]
Unlike mutable objects, immutable objects (such as strings, tuples) cannot be changed after creation — any operations create a new object.