Junior — Middle
How is the data structure array implemented in Python?
sobes.tech AI
Answer from AI
In Python, a data structure similar to an array is implemented as a list (list). Lists are dynamic arrays that can contain elements of different types and automatically resize when elements are added or removed.
Additionally, for working with fixed-type and fixed-size arrays, there is the array module, which provides more efficient storage of numeric data but with a restriction on element types.
Example of using a list:
arr = [1, 2, 3, 4]
arr.append(5) # adding an element
print(arr) # [1, 2, 3, 4, 5]
Example of using an array from the array module:
import array
arr = array.array('i', [1, 2, 3, 4]) # 'i' — type int
arr.append(5)
print(arr) # array('i', [1, 2, 3, 4, 5])