0% completed
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
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.
On this page
What Is an Array?
Key Points
Why Do We Need Arrays?
Memory Representation of an Array
How Arrays Are Stored in Memory
Memory Layout Example
Static vs. Dynamic Arrays
- Static Arrays
- Dynamic Arrays
- Static Vs. Dynamic Arrays Comparision
Which One Should You Use?
Basic Concepts and Operations in Arrays
- Accessing Elements
- Inserting Elements
- Deleting Elements
- Searching for an Element
- Updating an Element
Key Considerations for Arrays in Coding Interviews
- Validate Assumptions
- Handle Boundaries
- Efficiency Considerations
- Loops & Naming
- Algorithm Choice
- Testing & Edge Cases
- Handling Special Values
- Modifying While Iterating
- Array Methods