Bird
Raised Fist0
NumPydata~10 mins

Negative indexing in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Negative indexing
Start with array
↓
Choose index
↓
Is index negative?
No→Access element at index
Yes↓
Convert negative index to positive: length + index
↓
Access element at converted index
↓
Return element
Negative indexing means counting from the end of the array. If index is negative, add array length to get the positive index.
Execution Sample
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[-1])
print(arr[-3])
Access elements from the end of the array using negative indexes.
Execution Table
StepIndex UsedIs Negative?Converted IndexElement AccessedOutput
1-1Yes5 + (-1) = 4arr[4]50
2-3Yes5 + (-3) = 2arr[2]30
3EndN/AN/AN/AExecution ends
💡 All negative indexes converted and elements accessed; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
arr[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
indexN/A-1-3N/A
converted_indexN/A42N/A
outputN/A5030N/A
Key Moments - 2 Insights
Why do we add the array length to a negative index?
Because negative indexes count from the end, adding the array length converts them to the correct positive index (see execution_table steps 1 and 2).
What happens if the negative index is -1?
It accesses the last element because converted index is length - 1 (step 1 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what element does arr[-3] access?
A20
B30
C40
D50
💡 Hint
Check row 2 in execution_table where index -3 converts to 2 and accesses arr[2].
At which step does the negative index -1 get converted to a positive index?
AStep 1
BStep 2
CStep 3
DNo conversion needed
💡 Hint
See execution_table row 1 where index -1 is converted to 4.
If the array length was 6 instead of 5, what would arr[-1] access?
Aarr[4]
Barr[1]
Carr[5]
Darr[0]
💡 Hint
Negative index -1 converts to length + (-1), so 6 + (-1) = 5.
Concept Snapshot
Negative indexing in numpy:
- Use negative numbers to count from the end.
- Negative index i means access element at length + i.
- Example: arr[-1] is last element.
- Useful to quickly access elements from the back.
- Works like arr[len(arr) + index].
Full Transcript
Negative indexing lets you access elements from the end of a numpy array. When you use a negative index, numpy adds the array length to that index to find the correct position. For example, arr[-1] accesses the last element because it converts to arr[length - 1]. This is helpful to quickly get elements from the back without counting from zero. The execution steps show how negative indexes are converted and used to access elements.

Practice

(1/5)
1. What does negative indexing in a numpy array allow you to do?
easy
A. Sort the array in descending order
B. Access elements starting from the end of the array
C. Change the size of the array
D. Access elements only at the beginning of the array

Solution

  1. Step 1: Understand negative indexing concept

    Negative indexing means counting elements from the end of the array, starting at -1 for the last element.
  2. Step 2: Compare options with concept

    Only Access elements starting from the end of the array correctly describes accessing elements from the end using negative indexes.
  3. Final Answer:

    Access elements starting from the end of the array -> Option B
  4. Quick Check:

    Negative indexing = Access from end [OK]
Hint: Negative index -1 means last element [OK]
Common Mistakes:
  • Thinking negative index changes array size
  • Confusing negative indexing with sorting
  • Assuming negative index accesses start only at beginning
2. Which of the following is the correct syntax to get the last element of a numpy array arr using negative indexing?
easy
A. arr[0]
B. arr[1]
C. arr[-0]
D. arr[-1]

Solution

  1. Step 1: Recall negative index for last element

    In numpy, -1 is used to access the last element of an array.
  2. Step 2: Check each option

    arr[-1] uses arr[-1] which correctly accesses the last element. Others do not.
  3. Final Answer:

    arr[-1] -> Option D
  4. Quick Check:

    Last element = arr[-1] [OK]
Hint: Use arr[-1] for last element [OK]
Common Mistakes:
  • Using arr[-0] which is same as arr[0]
  • Using positive index 1 which is second element
  • Confusing zero index with negative zero
3. Given the numpy array arr = np.array([10, 20, 30, 40, 50]), what is the output of arr[-3]?
medium
A. 20
B. 40
C. 30
D. 50

Solution

  1. Step 1: Understand negative index -3

    Negative index -3 means counting from the end: last is -1 (50), second last -2 (40), third last -3 (30).
  2. Step 2: Identify element at index -3

    The element at arr[-3] is 30.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    arr[-3] = 30 [OK]
Hint: Count backward: -1 last, -2 second last, -3 third last [OK]
Common Mistakes:
  • Counting negative index from start instead of end
  • Mixing up index -3 with positive index 3
  • Off-by-one errors in counting
4. Consider the code snippet:
import numpy as np
arr = np.array([5, 10, 15, 20])
print(arr[-5])

What is the error in this code?
medium
A. IndexError: index -5 is out of bounds for axis 0 with size 4
B. SyntaxError due to negative index
C. Prints 5
D. Prints 20

Solution

  1. Step 1: Check array size and index used

    Array has 4 elements, valid negative indexes are -1 to -4.
  2. Step 2: Understand index -5 usage

    Index -5 is outside valid range, causing IndexError.
  3. Final Answer:

    IndexError: index -5 is out of bounds for axis 0 with size 4 -> Option A
  4. Quick Check:

    Negative index out of range = IndexError [OK]
Hint: Negative index must be >= -array length [OK]
Common Mistakes:
  • Assuming negative index wraps around infinitely
  • Thinking negative index causes syntax error
  • Expecting last element instead of error
5. You have a 2D numpy array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]). What does arr[-1, -2] return?
hard
A. 8
B. 7
C. 6
D. 9

Solution

  1. Step 1: Understand negative indexing in 2D arrays

    For rows, -1 means last row: [7, 8, 9]. For columns, -2 means second last column.
  2. Step 2: Identify element at arr[-1, -2]

    Last row is [7, 8, 9], second last column is element 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    arr[-1, -2] = 8 [OK]
Hint: Negative indexes count from end in each dimension [OK]
Common Mistakes:
  • Mixing row and column indexes
  • Using positive indexes instead of negative
  • Confusing which axis negative index applies to
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard