Junior — Middle
What mutable data structures are available in Python?
sobes.tech AI
Answer from AI
Python provides several mutable data structures that can be modified after creation:
- list — an ordered, mutable collection of elements. You can add, remove, and modify elements.
- dict — a dictionary that stores key-value pairs. It allows modifying, adding, and removing pairs.
- set — a collection of unique elements, mutable, supporting addition and removal operations.
- bytearray — a mutable sequence of bytes.
Example of using mutable structures:
my_list = [1, 2, 3]
my_list.append(4) # Adding an element
my_dict = {'a': 1, 'b': 2}
my_dict['c'] = 3 # Adding a key-value pair
my_set = {1, 2, 3}
my_set.add(4) # Adding an element
my_bytes = bytearray(b'abc')
my_bytes[0] = 100 # Changing the first byte
These structures are widely used for storing and processing data when content modification is required.