0
0
DSA Pythonprogramming

Array Declaration and Initialization in DSA Python

Choose your learning style9 modes available
Mental Model
An array is a row of boxes where each box holds one item, and we know exactly how many boxes there are from the start.
Analogy: Imagine a row of mailboxes, each labeled with a number. You can put one letter in each mailbox and find it easily by its number.
Index: 0   1   2   3   4
Array: [ ] [ ] [ ] [ ] [ ]
Dry Run Walkthrough
Input: Declare an array of size 5 and initialize it with values 10, 20, 30, 40, 50
Goal: Create an array with 5 boxes and put the given values in order
Step 1: Declare an array of size 5 with empty boxes
Index: 0   1   2   3   4
Array: [ ] [ ] [ ] [ ] [ ]
Why: We need to reserve space for 5 items before putting values
Step 2: Put 10 in box at index 0
Index: 0   1   2   3   4
Array: [10] [ ] [ ] [ ] [ ]
Why: Start filling the array from the first box
Step 3: Put 20 in box at index 1
Index: 0   1   2   3   4
Array: [10] [20] [ ] [ ] [ ]
Why: Continue filling the next box in order
Step 4: Put 30 in box at index 2
Index: 0   1   2   3   4
Array: [10] [20] [30] [ ] [ ]
Why: Fill the third box with the next value
Step 5: Put 40 in box at index 3
Index: 0   1   2   3   4
Array: [10] [20] [30] [40] [ ]
Why: Fill the fourth box with the next value
Step 6: Put 50 in box at index 4
Index: 0   1   2   3   4
Array: [10] [20] [30] [40] [50]
Why: Fill the last box with the last value
Result:
Index: 0   1   2   3   4
Array: [10] [20] [30] [40] [50]
Annotated Code
DSA Python
class Array:
    def __init__(self, size):
        self.size = size
        self.data = [None] * size  # create empty boxes

    def initialize(self, values):
        for i in range(min(self.size, len(values))):
            self.data[i] = values[i]  # put value in box at index i

    def __str__(self):
        return ' -> '.join(str(x) for x in self.data) + ' -> null'

# Driver code
arr = Array(5)  # declare array of size 5
arr.initialize([10, 20, 30, 40, 50])  # initialize with values
print(arr)
self.data = [None] * size # create empty boxes
reserve space for all boxes in the array
for i in range(min(self.size, len(values))):
loop through each index up to array size or values length
self.data[i] = values[i] # put value in box at index i
fill each box with the corresponding value
OutputSuccess
10 -> 20 -> 30 -> 40 -> 50 -> null
Complexity Analysis
Time: O(n) because we fill each of the n boxes once
Space: O(n) because we store n items in the array
vs Alternative: Compared to dynamic structures like linked lists, arrays use fixed space and allow fast access by index but need size known upfront
Edge Cases
Initialize with fewer values than size
Remaining boxes stay empty (None)
DSA Python
for i in range(min(self.size, len(values))):
Initialize with more values than size
Only first 'size' values are stored, extra values ignored
DSA Python
for i in range(min(self.size, len(values))):
Declare array of size zero
Creates empty array with no boxes
DSA Python
self.data = [None] * size
When to Use This Pattern
When you need to store multiple items in order and access them by position quickly, think of arrays because they give direct access by index.
Common Mistakes
Mistake: Trying to add more items than the declared size without resizing
Fix: Only fill up to the declared size or use a dynamic structure if size can change
Mistake: Not initializing the array before use, leading to errors
Fix: Always declare and initialize the array before accessing its elements
Summary
An array is a fixed-size collection of items stored in order.
Use arrays when you know the number of items in advance and want fast access by position.
The key is to reserve space first, then fill each box with the value you want.