How to Pair Elements with Rear elements in Matrix Row Using Python?

In some scenarios of programming, you might need to pair each element of a matrix row with the rear element (the element that appears immediately after it). This article explores various methods and examples to pair elements according to these conditions.

What is a Matrix?

Matrices are powerful data structures used to represent collections of elements organized in rows and columns. A matrix having n rows and m columns is called an n × m matrix.

Method 1: Using Nested Loops

This is a straightforward approach using nested loops to iterate over each row of the matrix. We iterate from the first element to the second-to-last element, since there's no subsequent element after the last one.

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def pair_with_rear_nested(matrix):
    for row in matrix:
        for i in range(len(row) - 1):
            curr = row[i]
            nxt = row[i + 1]
            print(f"Pair: {curr} - {nxt}")

pair_with_rear_nested(matrix)
Pair: 1 - 2
Pair: 2 - 3
Pair: 4 - 5
Pair: 5 - 6
Pair: 7 - 8
Pair: 8 - 9

Method 2: Using List Comprehension

This method uses list comprehension to create pairs more concisely. It traverses each row and pairs each element with its rear element using (row[i], row[i+1]).

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def pair_with_rear_comprehension(matrix):
    pairs = [(row[i], row[i + 1])
             for row in matrix
             for i in range(len(row) - 1)]
    
    for pair in pairs:
        print(f"Pair: {pair[0]} - {pair[1]}")

pair_with_rear_comprehension(matrix)
Pair: 1 - 2
Pair: 2 - 3
Pair: 4 - 5
Pair: 5 - 6
Pair: 7 - 8
Pair: 8 - 9

Method 3: Using zip() Function

This method uses the zip() function with list comprehension. The zip() function combines elements from the current row and a sliced row (starting from the second element) into tuples.

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def pair_with_rear_zip(matrix):
    pairs = [(curr, nxt)
             for row in matrix
             for curr, nxt in zip(row, row[1:])]
    
    for pair in pairs:
        print(f"Pair: {pair[0]} - {pair[1]}")

pair_with_rear_zip(matrix)
Pair: 1 - 2
Pair: 2 - 3
Pair: 4 - 5
Pair: 5 - 6
Pair: 7 - 8
Pair: 8 - 9

Method 4: Using NumPy Library

Using NumPy for efficient matrix processing, we first flatten the matrix into a 1D array using np.array(matrix).flatten(), then create pairs by iterating over the flattened array.

import numpy as np

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def pair_with_rear_numpy(matrix):
    flattened_matrix = np.array(matrix).flatten()
    pairs = [(flattened_matrix[i], flattened_matrix[i + 1])
             for i in range(len(flattened_matrix) - 1)]
    
    for pair in pairs:
        print(f"Pair: {pair[0]} - {pair[1]}")

pair_with_rear_numpy(matrix)
Pair: 1 - 2
Pair: 2 - 3
Pair: 3 - 4
Pair: 4 - 5
Pair: 5 - 6
Pair: 6 - 7
Pair: 7 - 8
Pair: 8 - 9

Method 5: Using Itertools Library

The itertools library provides chain.from_iterable() to flatten the matrix into a 1D iterable. We then create pairs by iterating over the flattened list.

import itertools

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def pair_with_rear_itertools(matrix):
    flattened_matrix = list(itertools.chain.from_iterable(matrix))
    pairs = [(flattened_matrix[i], flattened_matrix[i + 1])
             for i in range(len(flattened_matrix) - 1)]
    
    for pair in pairs:
        print(f"Pair: {pair[0]} - {pair[1]}")

pair_with_rear_itertools(matrix)
Pair: 1 - 2
Pair: 2 - 3
Pair: 3 - 4
Pair: 4 - 5
Pair: 5 - 6
Pair: 6 - 7
Pair: 7 - 8
Pair: 8 - 9

Comparison

Method Pairs Within Rows Only? Best For
Nested Loops Yes Simple, readable approach
List Comprehension Yes Concise, Pythonic code
zip() Function Yes Elegant pairing solution
NumPy No (across entire matrix) Large matrices, numerical computing
Itertools No (across entire matrix) Memory-efficient flattening

Conclusion

Choose the method that fits your needs: use nested loops or zip() for row-wise pairing, or NumPy/itertools for cross-row pairing. The zip() method provides the most elegant solution for row-wise pairing, while NumPy is ideal for numerical computations.

Updated on: 2026-03-27T14:58:53+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements