Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Iaroslav L
Python array from array module is not static

Iaroslav L

Oct 22, 2024

In the Array introduction section we have the following piece of code, saying that staticArray has fixed size:

from array import array # Static Array (Using the array module for strictly typed arrays; # however, lists are more commonly used and can be considered dynamic.) staticArray = array('i', [0]*5) # Array of integers with a fixed size of 5 staticArray[0] = 1

That's actually not true. Because it's still possible to append an item there:

staticArray.append(10) print(staticArray) # output: array('i', [1, 0, 0, 0, 0, 10])

0

0

Comments
Comments
Muhammad Alfian Rasyidin
Muhammad Alfian Rasyidin9 months ago

It seems to be a natural behavior of arrays in specific programming languages.

I believe, if we are careful to avoid appending to the variable that we want to treat as static, it could result in better performance since the array wouldn’t need to be resized.