0
0
DSA Pythonprogramming

Array Access and Update at Index in DSA Python

Choose your learning style9 modes available
Mental Model
An array is like a row of boxes where each box has a number. You can quickly find or change the number in any box by knowing its position.
Analogy: Imagine a row of mailboxes numbered from 0 upwards. To get or put mail, you just go to the mailbox number you want without opening others.
[0] -> 5 | [1] -> 10 | [2] -> 15 | [3] -> 20 | [4] -> 25
Dry Run Walkthrough
Input: array: [5, 10, 15, 20, 25], update index 2 with value 99
Goal: Change the value at position 2 to 99 and show the updated array
Step 1: Access the element at index 2
[0] -> 5 | [1] -> 10 | [2] -> 15 ↑ | [3] -> 20 | [4] -> 25
Why: We need to find the exact box to update
Step 2: Replace the value at index 2 with 99
[0] -> 5 | [1] -> 10 | [2] -> 99 ↑ | [3] -> 20 | [4] -> 25
Why: We update the box with the new value
Step 3: Finish update and show the full array
[0] -> 5 | [1] -> 10 | [2] -> 99 | [3] -> 20 | [4] -> 25
Why: The array now reflects the updated value
Result:
[0] -> 5 | [1] -> 10 | [2] -> 99 | [3] -> 20 | [4] -> 25
Annotated Code
DSA Python
class Array:
    def __init__(self, elements):
        self.data = elements

    def access(self, index):
        # Return the value at the given index
        return self.data[index]

    def update(self, index, value):
        # Change the value at the given index
        self.data[index] = value

    def __str__(self):
        # Format array for printing
        return ' | '.join(f'[{i}] -> {v}' for i, v in enumerate(self.data))


def main():
    arr = Array([5, 10, 15, 20, 25])
    print('Original array:')
    print(arr)

    # Access element at index 2
    val = arr.access(2)
    print(f'Value at index 2 before update: {val}')

    # Update element at index 2 to 99
    arr.update(2, 99)
    print('Array after update at index 2:')
    print(arr)


if __name__ == '__main__':
    main()
return self.data[index]
Access the value at the given index to read it
self.data[index] = value
Update the value at the given index to the new value
OutputSuccess
Original array: [0] -> 5 | [1] -> 10 | [2] -> 15 | [3] -> 20 | [4] -> 25 Value at index 2 before update: 15 Array after update at index 2: [0] -> 5 | [1] -> 10 | [2] -> 99 | [3] -> 20 | [4] -> 25
Complexity Analysis
Time: O(1) because accessing or updating an element by index in an array is direct and does not depend on array size
Space: O(n) because the array stores n elements in memory
vs Alternative: Compared to linked lists, arrays allow faster direct access and update by index without traversal
Edge Cases
Index is out of bounds (negative or >= array length)
Raises an IndexError to prevent invalid access
DSA Python
return self.data[index]
When to Use This Pattern
When you need to quickly get or change a value at a known position, use array access and update because it is fast and direct.
Common Mistakes
Mistake: Trying to access or update an index outside the array range
Fix: Always check that the index is within 0 and length-1 before accessing or updating
Summary
It lets you get or change the value at a specific position in an array quickly.
Use it when you know the exact position of the item you want to read or change.
The key is that arrays let you jump straight to any position without looking at others.