Junior — Middle
How to implement a dynamic array or list of elements in Python, considering the features of built-in data structures?
sobes.tech AI
Answer from AI
In Python, a dynamic array is implemented using the built-in data type list. It allows storing an ordered collection of elements, automatically expanding when new elements are added.
Example of using a list as a dynamic array:
arr = [] # empty list
arr.append(10) # add element
arr.append(20)
print(arr) # [10, 20]
# you can also modify elements by index
arr[0] = 5
print(arr) # [5, 20]
Lists in Python are convenient because they do not require pre-defining the size and support many operations for working with elements.