Start learning free

LESSON 258 OF 673 · NESTED LOOPS OVER GRIDS

Find in a Grid in Python: First Match in Reading Order

Practice Read the full explanation

Given a grid of integers grid and an integer target, find the first cell equal to target and return its position as a list of two integers: its row and its column.

A grid is a list of rows, and each row is a list of integers. Rows are numbered from 0, top to bottom, and columns from 0, left to right.

"First" means in reading order: the top row from left to right, then the next row, and so on. If no cell is equal to target, return [-1, -1].

 

Example 1:

Input: grid = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
], target = 6
Output: [1, 2]
Explanation: 6 is in row 1 (the second row) and column 2 (the third column).

Example 2:

Input: grid = [
  [5, 1],
  [1, 5]
], target = 1
Output: [0, 1]
Explanation: 1 appears twice. Reading row by row, the 1 in row 0, column 1 comes before the 1 in row 1, column 0.

Example 3:

Input: grid = [
  [4, 4],
  [4, 4]
], target = 3
Output: [-1, -1]
Explanation: No cell is equal to 3.

 

Constraints:

  • grid has at least 1 row and 1 column, and every row has the same number of values.
  • Every value and target is an integer between -1000 and 1000; they can be negative.
STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · NESTED LOOPS OVER GRIDS

    Find In Grid in Python, explained

    Problem overview

    The method findInGrid receives grid, a list of rows of integers, and an integer target. It returns the position of the first cell equal to target as a list of two integers: the row and the column. If no cell matches, it returns [-1, -1].

    "First" means reading order: the top row from left to right, then the next row, and so on. Rows are numbered from 0 at the top and columns from 0 at the left.

    In a grid with rows [1, 2, 3], [4, 5, 6] and [7, 8, 9], the value 6 is in row 1 and column 2, so the answer is [1, 2]. In [[5, 1], [1, 5]] the value 1 appears twice, and reading row by row the one at row 0, column 1 comes first: [0, 1]. In a grid of only 4s, searching for 3 gives [-1, -1].

    Approach

    Counting only needs the values, but this problem needs positions, so the loops must run over indexes, not over the values themselves. An outer loop goes through row numbers and an inner loop through the column numbers of that row. The inner loop finishes a whole row before the next row starts, which is the reading order the problem asks for.

    Because only the first match matters, the function can answer the moment it finds one. Everything after that cell is irrelevant. If both loops finish with no match, the answer is the "not found" pair.

    Step by step

    1. Loop row over range(len(grid)). len(grid) is the number of rows, because the grid is a list of rows.
    2. Inside it, loop col over range(len(grid[row])). grid[row] is one row, so its length is the number of columns.
    3. If grid[row][col] == target, return [row, col] immediately. return leaves the whole function, so both loops stop at the first match. A break would only end the inner loop.
    4. Return [-1, -1] after both loops. The function gets there only when no cell matched, and -1 is never a real row or column number.

    The solution in Python

    class Solution:
        def findInGrid(self, grid: List[List[int]], target: int) -> List[int]:
            for row in range(len(grid)):
                for col in range(len(grid[row])):
                    if grid[row][col] == target:
                        return [row, col]
            return [-1, -1]
    

    Python details:

    • Row index first. grid[row][col] picks row row and then the value at position col inside it. The answer [row, col] uses the same order.
    • range(n) counts from 0 to n - 1, which matches the 0-based numbering of rows and columns.
    • Returning a new list. Each return builds a fresh list, so the caller can change it without affecting anything else.

    Edge cases and common mistakes

    • Using break instead of return. It ends only the inner loop, so the outer loop moves on to the next row and keeps searching. The function then ends with the wrong answer, a later match or [-1, -1], even though a match was found.
    • Swapping row and column in the answer. For 6 in the first example, [2, 1] would be a different cell.
    • Several matches. Only the first in reading order is returned: for [[5, 1], [1, 5]] and target 1, the answer is [0, 1], not [1, 0].
    • Not found. For a grid with no match, both loops end and the function returns [-1, -1].
    • Target of 0 or a negative target. These are ordinary values. The not-found marker [-1, -1] is a list of two numbers, so it can never be confused with a real position.
    • Returning inside the wrong place. Putting return [-1, -1] inside the loops would give up after the first cell.

    Testing

    • The grid [[1, 2, 3], [4, 5, 6], [7, 8, 9]] with target 6 gives [1, 2].
    • [[5, 1], [1, 5]] with target 1 gives [0, 1].
    • [[4, 4], [4, 4]] with target 3 gives [-1, -1].
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free