Unique Values & Counting
Counting is the task of finding the distinct values in an array and tallying how often each one appears, which NumPy makes a one-liner with np.unique, np.bincount, and np.count_nonzero.
Learn Unique Values & Counting in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll extract sorted distinct values, get per-value counts with return_counts, histogram small integers with bincount, and count condition matches with count_nonzero.
np.unique(arr) returns the distinct values with duplicates removed, and the result is always sorted — even if the input was scrambled. It works on numbers and strings alike.
Add return_counts=True and np.unique hands back a second array: how many times each value occurred. Zip the two arrays together and you have an instant frequency table — the go-to way to count categories.
For non-negative integers , np.bincount is a fast specialist: it returns counts indexed by value, so result[k] is how many times k appeared. Separately, np.count_nonzero counts entries that are not zero — perfect for counting how many elements pass a condition.
Replace ___ with the keyword argument that makes np.unique also return per-value counts.
Answer: return_counts — it returns a counts array matching the sorted values.
✅ Fix: bincount needs non-negative integers; use np.unique(arr, return_counts=True) for other data.
✅ Fix: unpack both, e.g. values, counts = np.unique(arr, return_counts=True) .
✅ Fix: use return_index=True and sort by that index if you need first-appearance order.
Given a list of survey ratings, find which rating was most common and how many times it appeared, using unique with counts and argmax.
Lesson complete — you can count anything!
You can extract sorted distinct values with np.unique , tally categories with return_counts , histogram integers with np.bincount , and count matches with np.count_nonzero .
🚀 Up next: Set Operations — compare arrays with intersect, union, and difference.
Practice quiz
What does np.unique([3, 1, 2, 1, 3, 3]) return?
unique returns sorted distinct values with duplicates removed.
Is the output of np.unique always sorted?
- No, it keeps input order
- Yes, it is always sorted ascending
- Only for strings
- Only for integers
Answer: Yes, it is always sorted ascending. np.unique always returns its result sorted, even if the input was not.
How do you get per-value counts from np.unique?
- Pass return_counts=True
- Pass sort=True
- Use np.tally
- Pass count=True
Answer: Pass return_counts=True. return_counts=True returns the values plus a matching counts array.
What does np.bincount([0, 1, 1, 2, 1, 0]) return?
Index k holds the count of value k: two 0s, three 1s, one 2.
What does np.count_nonzero([55,80,42,91,67] >= 60) return?
- 5
- 2
- 3
- 0
Answer: 3. Three values (80, 91, 67) pass the >= 60 test.
When counts line up with np.unique's values, what order are they in?
- Input order
- Sorted value order
- Reverse order
- Random order
Answer: Sorted value order. Counts match the sorted values, so pair counts[i] with values[i].
Which is the fast specialist for counting non-negative integers?
- np.bincount
- np.sort
- np.where
- np.repeat
Answer: np.bincount. np.bincount counts each integer from 0 up to the maximum.
What does return_index=True give you from np.unique?
- The counts of each value
- The sorted values reversed
- Nothing extra
- The position of each value's first occurrence
Answer: The position of each value's first occurrence. return_index returns the index of the first occurrence of each value.
Why does np.bincount([0, -1, 2]) fail?
- The list is too short
- bincount needs non-negative integers
- It must be sorted first
- Floats are required
Answer: bincount needs non-negative integers. bincount only works on non-negative integers; negatives raise a ValueError.
On a boolean mask, how does count_nonzero compare to sum?
- sum is always larger
- count_nonzero rounds the result
- They give the same number (True counts as 1)
- count_nonzero only works on floats
Answer: They give the same number (True counts as 1). Both count the True entries, since True is 1.