1D Indexing
Read single elements of a 1D NumPy array with positive and negative indexes, and understand what an index really is.
Watch the lesson
1 video on CodeWithMunnaX
You have Virat Kohli's runs from 14 IPL matches stored in one NumPy array. Very often you don't want all 14 numbers — you want "the score in match 1", "the last match", or "the third-last match". Picking out one element by its position is called indexing, and it is the first skill you need before slicing, filtering and everything else in NumPy.
Definition
Indexing is selecting a single element of an array by writing its position number (index) inside square brackets, e.g.
arr[3]. In a 1D array, indexes start at 0 for the first element and go up to n − 1 for the last; negative indexes count backwards from the end, so-1is the last element.
- Position number (index) — an integer that says where the element sits, not what its value is.
- Starts at 0 — NumPy follows Python: the first element is
arr[0], notarr[1]. So "match 1" is index 0, "match 5" is index 4. - n − 1 — an array of size 14 has valid positive indexes 0 to 13. Index 14 does not exist.
- Negative index —
-1is the last element,-2the second-last, …-nis the first. Internally NumPy converts-kton - k.
Positions at a glance
text
value : 72 45 88 31 64 0 103 52 19 77 41 93 28 66
index : 0 1 2 3 4 5 6 7 8 9 10 11 12 13
neg : -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1Every element has two valid addresses: one positive, one negative. virat_runs[6] and virat_runs[-8] are the same element (103).
Syntax
python
arr[i] # element at position i (0-based)
arr[-k] # k-th element from the end| Part | Meaning | Default |
|---|---|---|
arr | any 1D NumPy array | — |
i | integer position from the start, 0 ≤ i ≤ n-1 | — (required) |
-k | integer position from the end, 1 ≤ k ≤ n | — |
The result of indexing a 1D array with one integer is a single scalar value (a NumPy scalar such as np.int64), not an array.
Example — first, last and any match
python
import numpy as np
virat_runs = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93, 28, 66])
print("Match 1 :", virat_runs[0])
print("Match 7 :", virat_runs[6])
print("Last :", virat_runs[-1])
print("2nd last :", virat_runs[-2])
print("Size :", virat_runs.size)
print("Same element?", virat_runs[6] == virat_runs[-8])text
Match 1 : 72
Match 7 : 103
Last : 66
2nd last : 28
Size : 14
Same element? TrueHow it works: "match 7" is index 6 because counting starts at 0. virat_runs[-8] means "8 from the end": 14 - 8 = 6, the same slot.
Example — what type do you get back?
python
virat_runs = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93, 28, 66])
score = virat_runs[0]
print(score)
print(type(score))
print(repr(score))
print(int(score) + 1)text
72
<class 'numpy.int64'>
np.int64(72)
73How it works: one integer index gives you one value, stored as a NumPy scalar of the array's dtype (np.int64 here). print() shows just 72, while repr() (what Jupyter shows when you type the expression alone) shows np.int64(72) in NumPy 2. It behaves like a normal number in maths; use int(...) if you need a plain Python int.
Example — index out of range
python
virat_runs = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93, 28, 66])
try:
print(virat_runs[14])
except IndexError as e:
print("IndexError:", e)text
IndexError: index 14 is out of bounds for axis 0 with size 14How it works: valid indexes for 14 elements are 0…13 (or -14…-1). Asking for 14 raises an IndexError — exactly like a Python list. (Slicing, covered next, is more forgiving.)
Python list vs NumPy (1D indexing)
| Python list | NumPy 1D array | |
|---|---|---|
| First element | lst[0] | arr[0] |
| Last element | lst[-1] | arr[-1] |
| Out of range | IndexError | IndexError |
| Type returned | the stored Python object | a NumPy scalar (np.int64, np.float64, …) |
| Pick many positions at once | ❌ not possible (lst[[0, 2]] fails) | ✅ arr[[0, 2]] — fancy indexing |
| Pick by condition | ❌ needs a loop / comprehension | ✅ arr[arr > 50] — boolean indexing |
Common mistakes
- ❌
virat_runs[1]for "match 1". ✅virat_runs[0]— the first element is at index 0. - ❌
virat_runs[14]for the last of 14 matches. ✅virat_runs[13]or, better,virat_runs[-1](works for any length). - ❌
virat_runs(0)with round brackets — arrays are not functions (TypeError: 'numpy.ndarray' object is not callable). ✅ Square bracketsvirat_runs[0]. - ❌ Using a float index like
virat_runs[2.0]. ✅ Indexes must be integers — NumPy raisesIndexError: only integers, slices ... are valid indices.
Interview tip
Q: What is arr[-1] and why is it useful?
A: It returns the last element. Negative indexes count from the end (-1 → n-1), so you can grab the latest value without knowing the array's length — handy for "the most recent match" or "today's price".
Key points to remember
- Indexing selects ONE element by its position: arr[i].
- Positions start at 0; the last valid positive index is n − 1.
- Negative indexes count from the end: -1 is the last element, -n is the first; NumPy turns -k into n - k.
- Indexing a 1D array with one integer returns a scalar (np.int64 / np.float64), not an array.
- An index outside -n … n-1 raises IndexError, just like a Python list.
- Indexes must be integers — floats like 2.0 are rejected.
Try it yourself
Positive and negative indexes
Out-of-range index raises IndexError
Practice: first vs last match
Revision Flashcards
Tap a card to flip it and see the answer.
