🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.unique()

AI & DATA SCIENCE // np-unique

np.unique() returns the sorted, unique elements of an array, with optional extra outputs for their original indices, inverse mapping, and occurrence counts.

Syntax

np.unique(arr, return_index=False, return_inverse=False, return_counts=False)

Deep Dive Course

By default, np.unique() flattens the input, removes duplicates, and returns the remaining distinct values in sorted order. Setting return_counts=True additionally returns how many times each unique value appeared in the original array, and setting return_index=True returns the index of the first occurrence of each unique value, which together make it a convenient one-call replacement for manually building a frequency table with a Python loop, at C-level speed.

1Understanding np.unique()

By default, np.unique() flattens the input, removes duplicates, and returns the remaining distinct values in sorted order. Setting return_counts=True additionally returns how many times each unique value appeared in the original array, and setting return_index=True returns the index of the first occurrence of each unique value, which together make it a convenient one-call replacement for manually building a frequency table with a Python loop, at C-level speed.

💡

Pass return_counts=True to get element frequencies directly, instead of writing a manual loop or dictionary to count occurrences — it's both faster and returns aligned arrays, unique values and their corresponding counts, ready for further NumPy processing.

editor.html
import numpy as np

arr = np.array([3, 1, 2, 3, 1, 1])
print(np.unique(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.unique() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([3, 1, 2, 3, 1, 1])
values, counts = np.unique(arr, return_counts=True)
print(values)
print(counts)
localhost:3000

3Best Practices

Follow these guidelines when working with np.unique():

1. Use return_counts=True for building frequency tables of array values instead of a manual counting loop

2. Remember np.unique() always returns results in sorted order, not the order values first appeared, which matters if original ordering is meaningful

3. Use np.unique() on flattened multi-dimensional data intentionally, since it collapses shape information by default — reshape the result afterward if the original structure needs to be preserved

⚠️

Tip: Pass return_counts=True to get element frequencies directly, instead of writing a manual loop or dictionary to count occurrences — it's both faster and returns aligned arrays, unique values and their corresponding counts, ready for further NumPy processing.

editor.html
import numpy as np

arr = np.array([3, 1, 2, 3, 1, 1])
print(np.unique(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([3, 1, 2, 3, 1, 1])
print(np.unique(arr))
Example 02Advanced Example
import numpy as np

arr = np.array([3, 1, 2, 3, 1, 1])
values, counts = np.unique(arr, return_counts=True)
print(values)
print(counts)

Best Practices

  • Use return_counts=True for building frequency tables of array values instead of a manual counting loop
  • Remember np.unique() always returns results in sorted order, not the order values first appeared, which matters if original ordering is meaningful
  • Use np.unique() on flattened multi-dimensional data intentionally, since it collapses shape information by default — reshape the result afterward if the original structure needs to be preserved

Interview Question

Why does np.unique() always return its results sorted, and how would you preserve the values' original first-appearance order instead?

Hint: Think about what return_index gives you and how you'd use it.

np.unique() sorts its output because the underlying algorithm identifies duplicates efficiently by first sorting the data, and returning that already-sorted result is simply the natural, efficient byproduct, rather than an explicit design goal in itself. To recover the values in their original first-appearance order instead, you'd call np.unique() with return_index=True to also get each unique value's first index in the original array, then sort the unique values by that index array to reconstruct the original appearance order.

Exercises

MediumPractice using np.unique() in a real scenario.
View Solution
import numpy as np

arr = np.array([3, 1, 2, 3, 1, 1])
print(np.unique(arr))

Frequently Asked Questions

Why does np.unique() always return its results sorted, and how would you preserve the values' original first-appearance order instead?

np.unique() sorts its output because the underlying algorithm identifies duplicates efficiently by first sorting the data, and returning that already-sorted result is simply the natural, efficient byproduct, rather than an explicit design goal in itself. To recover the values in their original first-appearance order instead, you'd call np.unique() with return_index=True to also get each unique value's first index in the original array, then sort the unique values by that index array to reconstruct the original appearance order.

Related Functions

setsnp-sortnp-count-nonzero