Python - Unique Values in Matrix

A matrix in Python is typically represented as a list of lists, where each inner list corresponds to a row. Finding unique values in a matrix is a common task in data processing and analysis. Python provides several efficient methods to extract unique elements from matrices.

Method 1: Using set()

The simplest approach is to iterate through all elements and add them to a set, which automatically removes duplicates ?

def unique_values_of_matrix(matrix):
    unique_elements = set()
    for row in matrix:
        for element in row:
            unique_elements.add(element)
    return unique_elements

# Example matrix
names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'John'],
    ['Sam', 'Tyler', 'Matt']
]

unique_values = unique_values_of_matrix(names)
print(sorted(unique_values))
['Daniel', 'Jack', 'John', 'Matt', 'Sam', 'Tyler']

Method 2: Using NumPy

NumPy provides the unique() function which efficiently finds unique values and returns them in sorted order ?

import numpy as np

names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'John'],
    ['Sam', 'Tyler', 'Matt']
]

# Convert to NumPy array and find unique values
matrix_array = np.array(names)
unique_values = np.unique(matrix_array)
print(unique_values)
['Daniel' 'Jack' 'John' 'Matt' 'Sam' 'Tyler']

Method 3: Using List Comprehension with Set

A more Pythonic approach using list comprehension to flatten the matrix and find unique values ?

names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'John'],
    ['Sam', 'Tyler', 'Matt']
]

# Flatten matrix and get unique values using set comprehension
unique_values = list({element for row in names for element in row})
print(sorted(unique_values))
['Daniel', 'Jack', 'John', 'Matt', 'Sam', 'Tyler']

Method 4: Using itertools.chain()

The chain() function flattens the matrix efficiently before extracting unique values ?

from itertools import chain

names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'John'],
    ['Sam', 'Tyler', 'Matt']
]

# Flatten using chain and find unique values
unique_values = set(chain(*names))
print(sorted(unique_values))
['Daniel', 'Jack', 'John', 'Matt', 'Sam', 'Tyler']

Method 5: Using Pandas

For large datasets, Pandas provides efficient methods to handle matrix operations ?

import pandas as pd

names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'John'],
    ['Sam', 'Tyler', 'Matt']
]

# Convert to DataFrame and find unique values
df = pd.DataFrame(names)
unique_values = df.stack().unique()
print(sorted(unique_values))
['Daniel', 'Jack', 'John', 'Matt', 'Sam', 'Tyler']

Performance Comparison

Method Best For Memory Usage Speed
set() Small matrices Low Fast
NumPy Numerical data Medium Very Fast
List Comprehension Pythonic code Low Fast
itertools.chain() Memory efficiency Very Low Fast
Pandas Large datasets High Fast

Conclusion

Use NumPy for numerical matrices and maximum performance. For simple cases, set() with nested loops provides clarity. Choose itertools.chain() for memory-efficient processing of large matrices.

Updated on: 2026-03-27T10:44:11+05:30

423 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements