You're building a seating chart for a theater. Each seat has a row and column. A nested list (list of lists) lets you store and access data by two coordinates - perfect for grids, game boards, and spreadsheets.

Create a seating chart

Initialize a 2D list to represent a grid of seats.

example
create_grid.py
Replay: real traced execution (multi-file project)
def main():
    # Theater seating chart: rows x seats per row
    rows = 3
    cols = 4

    # Create 2D list using list comprehension
    seating = [[0 for col in range(cols)] for row in range(rows)]

    # Assign seat numbers (1, 2, 3, ...)
    seat_number = 1
    for row in range(rows):
        for col in range(cols):
            seating[row][col] = seat_number
            seat_number += 1

    # Display the seating chart
    print("=== Theater Seating Chart ===")
    for row_idx, row in enumerate(seating):
        print(f"Row {row_idx + 1}: ", end="")
        for seat in row:
            print(f"{seat}\t", end="")
        print()

    total_seats = rows * cols
    print(f"Total seats: {total_seats}")

main()
def main():
    # Theater seating chart: rows x seats per row
    rows = 5
    cols = 4

    # Create 2D list using list comprehension
    seating = [[0 for col in range(cols)] for row in range(rows)]

    # Assign seat numbers (1, 2, 3, ...)
    seat_number = 1
    for row in range(rows):
        for col in range(cols):
            seating[row][col] = seat_number
            seat_number += 1

    # Display the seating chart
    print("=== Theater Seating Chart ===")
    for row_idx, row in enumerate(seating):
        print(f"Row {row_idx + 1}: ", end="")
        for seat in row:
            print(f"{seat}\t", end="")
        print()

    total_seats = rows * cols
    print(f"Total seats: {total_seats}")

main()
def main():
    # Theater seating chart: rows x seats per row
    rows = 3
    cols = 6

    # Create 2D list using list comprehension
    seating = [[0 for col in range(cols)] for row in range(rows)]

    # Assign seat numbers (1, 2, 3, ...)
    seat_number = 1
    for row in range(rows):
        for col in range(cols):
            seating[row][col] = seat_number
            seat_number += 1

    # Display the seating chart
    print("=== Theater Seating Chart ===")
    for row_idx, row in enumerate(seating):
        print(f"Row {row_idx + 1}: ", end="")
        for seat in row:
            print(f"{seat}\t", end="")
        print()

    total_seats = rows * cols
    print(f"Total seats: {total_seats}")

main()
  1. main()

    28main()29#@help seats
  2. rows ← 3, cols ← 4, seating ← [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

    1#@var=default,large2def main():3    # Theater seating chart: rows x seats per row4    rows→ 3 = 3  #@var=_,55    cols→ 4 = 4  #@var=_,66    7    # Create 2D list using list comprehension  #?seats8    seating→ [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] = [[0 for col in range(cols4)] for row in range(rows3)]9    10    # Assign seat numbers (1, 2, 3, ...)11    seat_number→ 1 = 112    for row in range(rows):
  3. for row in range(rows):

    pass 1 of 3
    11seat_number = 112for row0 in range(rows3):13    for col in range(cols):14        seating[row][col] = seat_number
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  4. seating[row][col] ← 1, seat_number ← 2

    pass 1 of 12
    12for row in range(rows):13    for col0 in range(cols4):14        seating[row][col]→ 1 = seat_number115        seat_number→ 2 += 1
    All 12 passes — pass 1 is the card above
    passcolseating[row][col]seat_number
    1011 2
    2122 3
    3233 4
    4344 5
    5055 6
    6166 7
    7277 8
    8388 9
    9099 10
    1011010 11
    1121111 12
    1231212 13
  5. print("=== Theater Seating Chart ===")

    17# Display the seating chart18print("=== Theater Seating Chart ===")19for row_idx, row in enumerate(seating):
    output=== Theater Seating Chart ===
  6. for row_idx, row in enumerate(seating):

    pass 1 of 3
    18print("=== Theater Seating Chart ===")19for row_idx0, row[1, 2, 3, 4] in enumerate(seating[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]):20    print(f"Row {row_idx0 + 1}: ", end="")21    for seat in row:
    outputRow 1: 
    All 3 passes — pass 1 is the card above
    passrow_idxrow
    10[1, 2, 3, 4]
    21[5, 6, 7, 8]
    32[9, 10, 11, 12]
  7. for seat in row:

    pass 1 of 12
    20print(f"Row {row_idx + 1}: ", end="")21for seat1 in row[1, 2, 3, 4]:22    print(f"{seat1}\t", end="")23print()
    output1	
    All 12 passes — pass 1 is the card above
    passseatrow
    11[1, 2, 3, 4]
    22[1, 2, 3, 4]
    33[1, 2, 3, 4]
    44[1, 2, 3, 4]
    55[5, 6, 7, 8]
    66[5, 6, 7, 8]
    77[5, 6, 7, 8]
    88[5, 6, 7, 8]
    99[9, 10, 11, 12]
    1010[9, 10, 11, 12]
    1111[9, 10, 11, 12]
    1212[9, 10, 11, 12]
  8. print()

    22    print(f"{seat}\t", end="")23print()
  9. print()

    22    print(f"{seat}\t", end="")23print()
  10. print()

    22    print(f"{seat}\t", end="")23print()
  11. total_seats ← 12

    25total_seats→ 12 = rows3 * cols426print(f"Total seats: {total_seats12}")
    outputTotal seats: 12
  12. main()

    28main()29#@help seats
  1. main()

    27main()
  2. rows ← 5, cols ← 4, seating ← [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

    1def main():2    # Theater seating chart: rows x seats per row3    rows→ 5 = 54    cols→ 4 = 45    6    # Create 2D list using list comprehension7    seating→ [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] = [[0 for col in range(cols4)] for row in range(rows5)]8    9    # Assign seat numbers (1, 2, 3, ...)10    seat_number→ 1 = 111    for row in range(rows):
  3. for row in range(rows):

    pass 1 of 5
    10seat_number = 111for row0 in range(rows5):12    for col in range(cols):13        seating[row][col] = seat_number
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  4. seating[row][col] ← 1, seat_number ← 2

    pass 1 of 20
    11for row in range(rows):12    for col0 in range(cols4):13        seating[row][col]→ 1 = seat_number114        seat_number→ 2 += 1
    20 passes — pass 1 is the card above
    passcolseating[row][col]seat_number
    1011 2
    2122 3
    3233 4
    4344 5
    5055 6
    6166 7
    7277 8
    8388 9
    9099 10
    ⋯ 9 more passes ⋯
    1921919 20
    2032020 21
  5. print("=== Theater Seating Chart ===")

    16# Display the seating chart17print("=== Theater Seating Chart ===")18for row_idx, row in enumerate(seating):
    output=== Theater Seating Chart ===
  6. for row_idx, row in enumerate(seating):

    pass 1 of 5
    17print("=== Theater Seating Chart ===")18for row_idx0, row[1, 2, 3, 4] in enumerate(seating[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]):19    print(f"Row {row_idx0 + 1}: ", end="")20    for seat in row:
    outputRow 1: 
    All 5 passes — pass 1 is the card above
    passrow_idxrow
    10[1, 2, 3, 4]
    21[5, 6, 7, 8]
    32[9, 10, 11, 12]
    43[13, 14, 15, 16]
    54[17, 18, 19, 20]
  7. for seat in row:

    pass 1 of 20
    19print(f"Row {row_idx + 1}: ", end="")20for seat1 in row[1, 2, 3, 4]:21    print(f"{seat1}\t", end="")22print()
    output1	
    20 passes — pass 1 is the card above
    passseatrow
    11[1, 2, 3, 4]
    22[1, 2, 3, 4]
    33[1, 2, 3, 4]
    44[1, 2, 3, 4]
    55[5, 6, 7, 8]
    66[5, 6, 7, 8]
    77[5, 6, 7, 8]
    88[5, 6, 7, 8]
    99[9, 10, 11, 12]
    ⋯ 9 more passes ⋯
    1919[17, 18, 19, 20]
    2020[17, 18, 19, 20]
  8. print()

    21    print(f"{seat}\t", end="")22print()
  9. print()

    21    print(f"{seat}\t", end="")22print()
  10. print()

    21    print(f"{seat}\t", end="")22print()
  11. print()

    21    print(f"{seat}\t", end="")22print()
  12. print()

    21    print(f"{seat}\t", end="")22print()
  13. total_seats ← 20

    24total_seats→ 20 = rows5 * cols425print(f"Total seats: {total_seats20}")
    outputTotal seats: 20
  14. main()

    27main()
  1. main()

    27main()
  2. rows ← 3, cols ← 6, seating ← [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]

    1def main():2    # Theater seating chart: rows x seats per row3    rows→ 3 = 34    cols→ 6 = 65    6    # Create 2D list using list comprehension7    seating→ [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] = [[0 for col in range(cols6)] for row in range(rows3)]8    9    # Assign seat numbers (1, 2, 3, ...)10    seat_number→ 1 = 111    for row in range(rows):
  3. for row in range(rows):

    pass 1 of 3
    10seat_number = 111for row0 in range(rows3):12    for col in range(cols):13        seating[row][col] = seat_number
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  4. seating[row][col] ← 1, seat_number ← 2

    pass 1 of 18
    11for row in range(rows):12    for col0 in range(cols6):13        seating[row][col]→ 1 = seat_number114        seat_number→ 2 += 1
    18 passes — pass 1 is the card above
    passcolseating[row][col]seat_number
    1011 2
    2122 3
    3233 4
    4344 5
    5455 6
    6566 7
    7077 8
    8188 9
    9299 10
    ⋯ 7 more passes ⋯
    1741717 18
    1851818 19
  5. print("=== Theater Seating Chart ===")

    16# Display the seating chart17print("=== Theater Seating Chart ===")18for row_idx, row in enumerate(seating):
    output=== Theater Seating Chart ===
  6. for row_idx, row in enumerate(seating):

    pass 1 of 3
    17print("=== Theater Seating Chart ===")18for row_idx0, row[1, 2, 3, 4, 5, 6] in enumerate(seating[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]]):19    print(f"Row {row_idx0 + 1}: ", end="")20    for seat in row:
    outputRow 1: 
    All 3 passes — pass 1 is the card above
    passrow_idxrow
    10[1, 2, 3, 4, 5, 6]
    21[7, 8, 9, 10, 11, 12]
    32[13, 14, 15, 16, 17, 18]
  7. for seat in row:

    pass 1 of 18
    19print(f"Row {row_idx + 1}: ", end="")20for seat1 in row[1, 2, 3, 4, 5, 6]:21    print(f"{seat1}\t", end="")22print()
    output1	
    18 passes — pass 1 is the card above
    passseatrow
    11[1, 2, 3, 4, 5, 6]
    22[1, 2, 3, 4, 5, 6]
    33[1, 2, 3, 4, 5, 6]
    44[1, 2, 3, 4, 5, 6]
    55[1, 2, 3, 4, 5, 6]
    66[1, 2, 3, 4, 5, 6]
    77[7, 8, 9, 10, 11, 12]
    88[7, 8, 9, 10, 11, 12]
    99[7, 8, 9, 10, 11, 12]
    ⋯ 7 more passes ⋯
    1717[13, 14, 15, 16, 17, 18]
    1818[13, 14, 15, 16, 17, 18]
  8. print()

    21    print(f"{seat}\t", end="")22print()
  9. print()

    21    print(f"{seat}\t", end="")22print()
  10. print()

    21    print(f"{seat}\t", end="")22print()
  11. total_seats ← 18

    24total_seats→ 18 = rows3 * cols625print(f"Total seats: {total_seats18}")
    outputTotal seats: 18
  12. main()

    27main()

A 2D list is a list of lists. Use list comprehension to create grids.

nested list List of lists: `grid = [[1,2], [3,4]]`. Access with `grid[row][col]`.

Access a specific seat

Look up a value using row and column indices.

example
access_element.py
Replay: real traced execution (multi-file project)
def main():
    # Ticket prices by section (rows) and day (columns)
    # Rows: 0=Front, 1=Middle, 2=Back
    # Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri
    prices = [
        [50, 50, 55, 55, 75],   # Front row
        [40, 40, 45, 45, 60],   # Middle row
        [25, 25, 30, 30, 45]    # Back row
    ]

    # Customer wants: middle section, Friday
    want_row = 1
    want_col = 4

    price = prices[want_row][want_col]

    sections = ["Front", "Middle", "Back"]
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

    print("=== Ticket Lookup ===")
    print(f"Section: {sections[want_row]}")
    print(f"Day: {days[want_col]}")
    print(f"Price: ${price}")

    # Show full price chart
    print("\n=== Full Price Chart ===")
    header = "        " + "\t".join(day[:3] for day in days)
    print(header)

    for r, row in enumerate(prices):
        row_str = "\t".join(f"${p}" for p in row)
        print(f"{sections[r]}\t{row_str}")

main()
def main():
    # Ticket prices by section (rows) and day (columns)
    # Rows: 0=Front, 1=Middle, 2=Back
    # Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri
    prices = [
        [50, 50, 55, 55, 75],   # Front row
        [40, 40, 45, 45, 60],   # Middle row
        [25, 25, 30, 30, 45]    # Back row
    ]

    # Customer wants: middle section, Friday
    want_row = 0
    want_col = 4

    price = prices[want_row][want_col]

    sections = ["Front", "Middle", "Back"]
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

    print("=== Ticket Lookup ===")
    print(f"Section: {sections[want_row]}")
    print(f"Day: {days[want_col]}")
    print(f"Price: ${price}")

    # Show full price chart
    print("\n=== Full Price Chart ===")
    header = "        " + "\t".join(day[:3] for day in days)
    print(header)

    for r, row in enumerate(prices):
        row_str = "\t".join(f"${p}" for p in row)
        print(f"{sections[r]}\t{row_str}")

main()
def main():
    # Ticket prices by section (rows) and day (columns)
    # Rows: 0=Front, 1=Middle, 2=Back
    # Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri
    prices = [
        [50, 50, 55, 55, 75],   # Front row
        [40, 40, 45, 45, 60],   # Middle row
        [25, 25, 30, 30, 45]    # Back row
    ]

    # Customer wants: middle section, Friday
    want_row = 1
    want_col = 0

    price = prices[want_row][want_col]

    sections = ["Front", "Middle", "Back"]
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

    print("=== Ticket Lookup ===")
    print(f"Section: {sections[want_row]}")
    print(f"Day: {days[want_col]}")
    print(f"Price: ${price}")

    # Show full price chart
    print("\n=== Full Price Chart ===")
    header = "        " + "\t".join(day[:3] for day in days)
    print(header)

    for r, row in enumerate(prices):
        row_str = "\t".join(f"${p}" for p in row)
        print(f"{sections[r]}\t{row_str}")

main()
  1. main()

    35main()36#@help rowcol
  2. prices ← [[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]]

    1#@var=default,corner2def main():3    # Ticket prices by section (rows) and day (columns)4    # Rows: 0=Front, 1=Middle, 2=Back5    # Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri6    prices→ [[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]] = [7        [50, 50, 55, 55, 75],   # Front row8        [40, 40, 45, 45, 60],   # Middle row  9        [25, 25, 30, 30, 45]    # Back row10    ]11    12    # Customer wants: middle section, Friday13    want_row→ 1 = 1  #?rowcol  #@var=_,014    want_col→ 4 = 4            #@var=_,015    16    price→ 60 = prices[want_row][want_col]6017    18    sections→ ['Front', 'Middle', 'Back'] = ["Front", "Middle", "Back"]19    days→ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]20    21    print("=== Ticket Lookup ===")22    print(f"Section: {sections[want_row]Middle}")23    print(f"Day: {days[want_col]Friday}")24    print(f"Price: ${price60}")25    26    # Show full price chart27    print("\n=== Full Price Chart ===")28    header→         Mon	Tue	Wed	Thu	Fri = "        " + "\t".join(day[:3](empty) for day in days['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])29    print(header        Mon	Tue	Wed	Thu	Fri)
    output=== Ticket Lookup ===
    Section: Middle
    Day: Friday
    Price: $60
    
    === Full Price Chart ===
            Mon	Tue	Wed	Thu	Fri
  3. row_str ← $50 $50 $55 $55 $75

    pass 1 of 3
    31for r0, row[50, 50, 55, 55, 75] in enumerate(prices[[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]]):32    row_str→ $50	$50	$55	$55	$75 = "\t".join(f"${p}" for p in row[50, 50, 55, 55, 75])33    print(f"{sections[r]Front}\t{row_str$50	$50	$55	$55	$75}")
    outputFront	$50	$50	$55	$55	$75
    All 3 passes — pass 1 is the card above
    passrrowsections[r]row_str
    10[50, 50, 55, 55, 75]Front$50 $50 $55 $55 $75
    21[40, 40, 45, 45, 60]Middle$40 $40 $45 $45 $60
    32[25, 25, 30, 30, 45]Back$25 $25 $30 $30 $45
  4. main()

    35main()36#@help rowcol
  1. main()

    34main()
  2. prices ← [[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]]

    1def main():2    # Ticket prices by section (rows) and day (columns)3    # Rows: 0=Front, 1=Middle, 2=Back4    # Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri5    prices→ [[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]] = [6        [50, 50, 55, 55, 75],   # Front row7        [40, 40, 45, 45, 60],   # Middle row  8        [25, 25, 30, 30, 45]    # Back row9    ]10    11    # Customer wants: middle section, Friday12    want_row→ 0 = 013    want_col→ 4 = 414    15    price→ 75 = prices[want_row][want_col]7516    17    sections→ ['Front', 'Middle', 'Back'] = ["Front", "Middle", "Back"]18    days→ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]19    20    print("=== Ticket Lookup ===")21    print(f"Section: {sections[want_row]Front}")22    print(f"Day: {days[want_col]Friday}")23    print(f"Price: ${price75}")24    25    # Show full price chart26    print("\n=== Full Price Chart ===")27    header→         Mon	Tue	Wed	Thu	Fri = "        " + "\t".join(day[:3](empty) for day in days['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])28    print(header        Mon	Tue	Wed	Thu	Fri)
    output=== Ticket Lookup ===
    Section: Front
    Day: Friday
    Price: $75
    
    === Full Price Chart ===
            Mon	Tue	Wed	Thu	Fri
  3. row_str ← $50 $50 $55 $55 $75

    pass 1 of 3
    30for r0, row[50, 50, 55, 55, 75] in enumerate(prices[[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]]):31    row_str→ $50	$50	$55	$55	$75 = "\t".join(f"${p}" for p in row[50, 50, 55, 55, 75])32    print(f"{sections[r]Front}\t{row_str$50	$50	$55	$55	$75}")
    outputFront	$50	$50	$55	$55	$75
    All 3 passes — pass 1 is the card above
    passrrowsections[r]row_str
    10[50, 50, 55, 55, 75]Front$50 $50 $55 $55 $75
    21[40, 40, 45, 45, 60]Middle$40 $40 $45 $45 $60
    32[25, 25, 30, 30, 45]Back$25 $25 $30 $30 $45
  4. main()

    34main()
  1. main()

    34main()
  2. prices ← [[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]]

    1def main():2    # Ticket prices by section (rows) and day (columns)3    # Rows: 0=Front, 1=Middle, 2=Back4    # Cols: 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri5    prices→ [[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]] = [6        [50, 50, 55, 55, 75],   # Front row7        [40, 40, 45, 45, 60],   # Middle row  8        [25, 25, 30, 30, 45]    # Back row9    ]10    11    # Customer wants: middle section, Friday12    want_row→ 1 = 113    want_col→ 0 = 014    15    price→ 40 = prices[want_row][want_col]4016    17    sections→ ['Front', 'Middle', 'Back'] = ["Front", "Middle", "Back"]18    days→ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]19    20    print("=== Ticket Lookup ===")21    print(f"Section: {sections[want_row]Middle}")22    print(f"Day: {days[want_col]Monday}")23    print(f"Price: ${price40}")24    25    # Show full price chart26    print("\n=== Full Price Chart ===")27    header→         Mon	Tue	Wed	Thu	Fri = "        " + "\t".join(day[:3](empty) for day in days['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])28    print(header        Mon	Tue	Wed	Thu	Fri)
    output=== Ticket Lookup ===
    Section: Middle
    Day: Monday
    Price: $40
    
    === Full Price Chart ===
            Mon	Tue	Wed	Thu	Fri
  3. row_str ← $50 $50 $55 $55 $75

    pass 1 of 3
    30for r0, row[50, 50, 55, 55, 75] in enumerate(prices[[50, 50, 55, 55, 75], [40, 40, 45, 45, 60], [25, 25, 30, 30, 45]]):31    row_str→ $50	$50	$55	$55	$75 = "\t".join(f"${p}" for p in row[50, 50, 55, 55, 75])32    print(f"{sections[r]Front}\t{row_str$50	$50	$55	$55	$75}")
    outputFront	$50	$50	$55	$55	$75
    All 3 passes — pass 1 is the card above
    passrrowsections[r]row_str
    10[50, 50, 55, 55, 75]Front$50 $50 $55 $55 $75
    21[40, 40, 45, 45, 60]Middle$40 $40 $45 $45 $60
    32[25, 25, 30, 30, 45]Back$25 $25 $30 $30 $45
  4. main()

    34main()

Access with two indices: grid[1][2] means row 1, column 2.

Mark a seat as reserved

Update a value at a specific position.

example
update_cell.py
Replay: real traced execution (multi-file project)
def main():
    # Seating chart: 0 = available, 1 = reserved
    seats = [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    # Customer reservation (row, col)
    reserve_row = 1
    reserve_col = 2

    print("=== Before Reservation ===")
    print_seating(seats)

    # Make the reservation
    seats[reserve_row][reserve_col] = 1

    print(f"\n=== After Reserving Row {reserve_row + 1}, Seat {reserve_col + 1} ===")
    print_seating(seats)

    # Reserve more seats
    seats[0][0] = 1
    seats[2][3] = 1

    print("\n=== After Additional Reservations ===")
    print_seating(seats)

    # Count available seats using sum with generator
    available = sum(seat == 0 for row in seats for seat in row)
    print(f"\nAvailable seats: {available}")

def print_seating(seats):
    print("  1 2 3 4")
    for r, row in enumerate(seats):
        symbols = ["O" if s == 0 else "X" for s in row]
        print(f"{r + 1} {' '.join(symbols)}")

main()
def main():
    # Seating chart: 0 = available, 1 = reserved
    seats = [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    # Customer reservation (row, col)
    reserve_row = 0
    reserve_col = 2

    print("=== Before Reservation ===")
    print_seating(seats)

    # Make the reservation
    seats[reserve_row][reserve_col] = 1

    print(f"\n=== After Reserving Row {reserve_row + 1}, Seat {reserve_col + 1} ===")
    print_seating(seats)

    # Reserve more seats
    seats[0][0] = 1
    seats[2][3] = 1

    print("\n=== After Additional Reservations ===")
    print_seating(seats)

    # Count available seats using sum with generator
    available = sum(seat == 0 for row in seats for seat in row)
    print(f"\nAvailable seats: {available}")

def print_seating(seats):
    print("  1 2 3 4")
    for r, row in enumerate(seats):
        symbols = ["O" if s == 0 else "X" for s in row]
        print(f"{r + 1} {' '.join(symbols)}")

main()
def main():
    # Seating chart: 0 = available, 1 = reserved
    seats = [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    # Customer reservation (row, col)
    reserve_row = 1
    reserve_col = 1

    print("=== Before Reservation ===")
    print_seating(seats)

    # Make the reservation
    seats[reserve_row][reserve_col] = 1

    print(f"\n=== After Reserving Row {reserve_row + 1}, Seat {reserve_col + 1} ===")
    print_seating(seats)

    # Reserve more seats
    seats[0][0] = 1
    seats[2][3] = 1

    print("\n=== After Additional Reservations ===")
    print_seating(seats)

    # Count available seats using sum with generator
    available = sum(seat == 0 for row in seats for seat in row)
    print(f"\nAvailable seats: {available}")

def print_seating(seats):
    print("  1 2 3 4")
    for r, row in enumerate(seats):
        symbols = ["O" if s == 0 else "X" for s in row]
        print(f"{r + 1} {' '.join(symbols)}")

main()
  1. main()

    40main()41#@help update
  2. seats ← [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], reserve_row ← 1

    1#@var=default,multiple2def main():3    # Seating chart: 0 = available, 1 = reserved4    seats→ [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] = [5        [0, 0, 0, 0],6        [0, 0, 0, 0],7        [0, 0, 0, 0]8    ]9    10    # Customer reservation (row, col)11    reserve_row→ 1 = 1  #@var=_,012    reserve_col→ 2 = 2  #@var=_,113    14    print("=== Before Reservation ===")15    print_seating(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
    output=== Before Reservation ===
  3. def print_seating(seats):

    pass 1 of 3
    34def print_seating(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]):35    print("  1 2 3 4")36    for r, row in enumerate(seats):
    output  1 2 3 4
    All 3 passes — pass 1 is the card above
    passseats
    1[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    2[[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
    3[[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
  4. symbols ← ['O', 'O', 'O', 'O']

    pass 1 of 9
    35print("  1 2 3 4")36for r0, row[0, 0, 0, 0] in enumerate(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]):37    symbols→ ['O', 'O', 'O', 'O'] = ["O" if s == 0 else "X" for s in row[0, 0, 0, 0]]38    print(f"{r0 + 1} {' '.join(symbols['O', 'O', 'O', 'O'])}")
    output1 O O O O
    All 9 passes — pass 1 is the card above
    passrrowseatsreserve_rowreserve_colsymbolsseats[reserve_row][reserve_col]seats[0][0]seats[2][3]available
    10[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    21[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    32[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]12['O', 'O', 'O', 'O']1
    40[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    51[0, 0, 1, 0][[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]['O', 'O', 'X', 'O']
    62[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']11
    70[1, 0, 0, 0][[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]['X', 'O', 'O', 'O']
    81[0, 0, 1, 0][[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]['O', 'O', 'X', 'O']
    92[0, 0, 0, 1][[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]['O', 'O', 'O', 'X']9
  5. main()

    40main()41#@help update
  1. main()

    39main()
  2. seats ← [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], reserve_row ← 0

    1def main():2    # Seating chart: 0 = available, 1 = reserved3    seats→ [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] = [4        [0, 0, 0, 0],5        [0, 0, 0, 0],6        [0, 0, 0, 0]7    ]8    9    # Customer reservation (row, col)10    reserve_row→ 0 = 011    reserve_col→ 2 = 212    13    print("=== Before Reservation ===")14    print_seating(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
    output=== Before Reservation ===
  3. def print_seating(seats):

    pass 1 of 3
    33def print_seating(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]):34    print("  1 2 3 4")35    for r, row in enumerate(seats):
    output  1 2 3 4
    All 3 passes — pass 1 is the card above
    passseats
    1[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    2[[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    3[[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
  4. symbols ← ['O', 'O', 'O', 'O']

    pass 1 of 9
    34print("  1 2 3 4")35for r0, row[0, 0, 0, 0] in enumerate(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]):36    symbols→ ['O', 'O', 'O', 'O'] = ["O" if s == 0 else "X" for s in row[0, 0, 0, 0]]37    print(f"{r0 + 1} {' '.join(symbols['O', 'O', 'O', 'O'])}")
    output1 O O O O
    All 9 passes — pass 1 is the card above
    passrrowseatsreserve_rowreserve_colsymbolsseats[reserve_row][reserve_col]seats[0][0]seats[2][3]available
    10[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    21[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    32[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]02['O', 'O', 'O', 'O']1
    40[0, 0, 1, 0][[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'X', 'O']
    51[0, 0, 0, 0][[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    62[0, 0, 0, 0][[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']11
    70[1, 0, 1, 0][[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]['X', 'O', 'X', 'O']
    81[0, 0, 0, 0][[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]['O', 'O', 'O', 'O']
    92[0, 0, 0, 1][[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]['O', 'O', 'O', 'X']9
  5. main()

    39main()
  1. main()

    39main()
  2. seats ← [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], reserve_row ← 1

    1def main():2    # Seating chart: 0 = available, 1 = reserved3    seats→ [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] = [4        [0, 0, 0, 0],5        [0, 0, 0, 0],6        [0, 0, 0, 0]7    ]8    9    # Customer reservation (row, col)10    reserve_row→ 1 = 111    reserve_col→ 1 = 112    13    print("=== Before Reservation ===")14    print_seating(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
    output=== Before Reservation ===
  3. def print_seating(seats):

    pass 1 of 3
    33def print_seating(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]):34    print("  1 2 3 4")35    for r, row in enumerate(seats):
    output  1 2 3 4
    All 3 passes — pass 1 is the card above
    passseats
    1[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    2[[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]
    3[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
  4. symbols ← ['O', 'O', 'O', 'O']

    pass 1 of 9
    34print("  1 2 3 4")35for r0, row[0, 0, 0, 0] in enumerate(seats[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]):36    symbols→ ['O', 'O', 'O', 'O'] = ["O" if s == 0 else "X" for s in row[0, 0, 0, 0]]37    print(f"{r0 + 1} {' '.join(symbols['O', 'O', 'O', 'O'])}")
    output1 O O O O
    All 9 passes — pass 1 is the card above
    passrrowseatsreserve_rowreserve_colsymbolsseats[reserve_row][reserve_col]seats[0][0]seats[2][3]available
    10[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    21[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    32[0, 0, 0, 0][[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]11['O', 'O', 'O', 'O']1
    40[0, 0, 0, 0][[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']
    51[0, 1, 0, 0][[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]['O', 'X', 'O', 'O']
    62[0, 0, 0, 0][[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]['O', 'O', 'O', 'O']11
    70[1, 0, 0, 0][[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]['X', 'O', 'O', 'O']
    81[0, 1, 0, 0][[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]['O', 'X', 'O', 'O']
    92[0, 0, 0, 1][[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]['O', 'O', 'O', 'X']9
  5. main()

    39main()

Assign directly: grid[row][col] = value. Lists are mutable.

Sum all values

Calculate a total by traversing every cell.

data
sum_all.py
Replay: real traced execution (multi-file project)
def main():
    # Monthly sales by product (in units)
    # Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)
    # Cols: Months (0=Jan, 1=Feb, 2=Mar)
    sales = [
        [120, 135, 150],  # Widgets
        [80, 95, 110],    # Gadgets
        [200, 180, 220]   # Gizmos
    ]

    # Quarterly sales (4 products, 4 quarters)
    quarterly = [
        [1200, 1350, 1500, 1400],
        [800, 950, 1100, 1050],
        [2000, 1800, 2200, 2100],
        [500, 600, 700, 650]
    ]

    data = sales

    # Calculate grand total - Pythonic way!
    grand_total = sum(sum(row) for row in data)

    print("=== Sales Summary ===")
    print(f"Grand Total: {grand_total} units")

    # Calculate row totals (per product)
    print("\n=== Per Product ===")
    products = ["Widgets", "Gadgets", "Gizmos", "Sprockets"]
    for i, row in enumerate(data):
        row_total = sum(row)
        print(f"{products[i]}: {row_total}")

    # Calculate column totals (per period)
    print("\n=== Per Period ===")
    num_periods = len(data[0])
    for col in range(num_periods):
        col_total = sum(row[col] for row in data)
        print(f"Period {col + 1}: {col_total}")

main()
def main():
    # Monthly sales by product (in units)
    # Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)
    # Cols: Months (0=Jan, 1=Feb, 2=Mar)
    sales = [
        [120, 135, 150],  # Widgets
        [80, 95, 110],    # Gadgets
        [200, 180, 220]   # Gizmos
    ]

    # Quarterly sales (4 products, 4 quarters)
    quarterly = [
        [1200, 1350, 1500, 1400],
        [800, 950, 1100, 1050],
        [2000, 1800, 2200, 2100],
        [500, 600, 700, 650]
    ]

    data = quarterly

    # Calculate grand total - Pythonic way!
    grand_total = sum(sum(row) for row in data)

    print("=== Sales Summary ===")
    print(f"Grand Total: {grand_total} units")

    # Calculate row totals (per product)
    print("\n=== Per Product ===")
    products = ["Widgets", "Gadgets", "Gizmos", "Sprockets"]
    for i, row in enumerate(data):
        row_total = sum(row)
        print(f"{products[i]}: {row_total}")

    # Calculate column totals (per period)
    print("\n=== Per Period ===")
    num_periods = len(data[0])
    for col in range(num_periods):
        col_total = sum(row[col] for row in data)
        print(f"Period {col + 1}: {col_total}")

main()
  1. main()

    42main()43#@help sum
  2. sales ← [[120, 135, 150], [80, 95, 110], [200, 180, 220]], quarterly ← [[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]]

    1#@var=default,quarterly2def main():3    # Monthly sales by product (in units)4    # Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)5    # Cols: Months (0=Jan, 1=Feb, 2=Mar)6    sales→ [[120, 135, 150], [80, 95, 110], [200, 180, 220]] = [       #@var=_,!7        [120, 135, 150],  # Widgets8        [80, 95, 110],    # Gadgets9        [200, 180, 220]   # Gizmos10    ]11    12    # Quarterly sales (4 products, 4 quarters)13    quarterly→ [[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]] = [   #@var=!,_14        [1200, 1350, 1500, 1400],15        [800, 950, 1100, 1050],16        [2000, 1800, 2200, 2100],17        [500, 600, 700, 650]18    ]19    20    data→ [[120, 135, 150], [80, 95, 110], [200, 180, 220]] = sales[[120, 135, 150], [80, 95, 110], [200, 180, 220]]  #@var=_,quarterly21    22    # Calculate grand total - Pythonic way!  #?sum23    grand_total→ 1290 = sum(sum(row) for row in data[[120, 135, 150], [80, 95, 110], [200, 180, 220]])24    25    print("=== Sales Summary ===")26    print(f"Grand Total: {grand_total1290} units")27    28    # Calculate row totals (per product)29    print("\n=== Per Product ===")30    products→ ['Widgets', 'Gadgets', 'Gizmos', 'Sprockets'] = ["Widgets", "Gadgets", "Gizmos", "Sprockets"]31    for i, row in enumerate(data):
    output=== Sales Summary ===
    Grand Total: 1290 units
    
    === Per Product ===
  3. row_total ← 405

    pass 1 of 3
    30products = ["Widgets", "Gadgets", "Gizmos", "Sprockets"]31for i0, row[120, 135, 150] in enumerate(data[[120, 135, 150], [80, 95, 110], [200, 180, 220]]):32    row_total→ 405 = sum(row[120, 135, 150])33    print(f"{products[i]Widgets}: {row_total405}")
    outputWidgets: 405
    All 3 passes — pass 1 is the card above
    passirowproducts[i]row_total
    10[120, 135, 150]Widgets405
    21[80, 95, 110]Gadgets285
    32[200, 180, 220]Gizmos600
  4. num_periods ← 3

    35# Calculate column totals (per period)36print("\n=== Per Period ===")37num_periods→ 3 = len(data[0][120, 135, 150])38for col in range(num_periods):
    output
    === Per Period ===
  5. col_total ← 400

    pass 1 of 3
    37num_periods = len(data[0])38for col0 in range(num_periods3):39    col_total→ 400 = sum(row[col]200 for row in data[[120, 135, 150], [80, 95, 110], [200, 180, 220]])40    print(f"Period {col0 + 1}: {col_total400}")
    outputPeriod 1: 400
    All 3 passes — pass 1 is the card above
    passcolrow[col]col_total
    10200400
    21180410
    32220480
  6. main()

    42main()43#@help sum
  1. main()

    41main()
  2. sales ← [[120, 135, 150], [80, 95, 110], [200, 180, 220]], quarterly ← [[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]]

    1def main():2    # Monthly sales by product (in units)3    # Rows: Products (0=Widgets, 1=Gadgets, 2=Gizmos)4    # Cols: Months (0=Jan, 1=Feb, 2=Mar)5    sales→ [[120, 135, 150], [80, 95, 110], [200, 180, 220]] = [6        [120, 135, 150],  # Widgets7        [80, 95, 110],    # Gadgets8        [200, 180, 220]   # Gizmos9    ]10    11    # Quarterly sales (4 products, 4 quarters)12    quarterly→ [[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]] = [13        [1200, 1350, 1500, 1400],14        [800, 950, 1100, 1050],15        [2000, 1800, 2200, 2100],16        [500, 600, 700, 650]17    ]18    19    data→ [[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]] = quarterly[[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]]20    21    # Calculate grand total - Pythonic way!22    grand_total→ 19900 = sum(sum(row) for row in data[[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]])23    24    print("=== Sales Summary ===")25    print(f"Grand Total: {grand_total19900} units")26    27    # Calculate row totals (per product)28    print("\n=== Per Product ===")29    products→ ['Widgets', 'Gadgets', 'Gizmos', 'Sprockets'] = ["Widgets", "Gadgets", "Gizmos", "Sprockets"]30    for i, row in enumerate(data):
    output=== Sales Summary ===
    Grand Total: 19900 units
    
    === Per Product ===
  3. row_total ← 5450

    pass 1 of 4
    29products = ["Widgets", "Gadgets", "Gizmos", "Sprockets"]30for i0, row[1200, 1350, 1500, 1400] in enumerate(data[[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]]):31    row_total→ 5450 = sum(row[1200, 1350, 1500, 1400])32    print(f"{products[i]Widgets}: {row_total5450}")
    outputWidgets: 5450
    All 4 passes — pass 1 is the card above
    passirowproducts[i]row_total
    10[1200, 1350, 1500, 1400]Widgets5450
    21[800, 950, 1100, 1050]Gadgets3900
    32[2000, 1800, 2200, 2100]Gizmos8100
    43[500, 600, 700, 650]Sprockets2450
  4. num_periods ← 4

    34# Calculate column totals (per period)35print("\n=== Per Period ===")36num_periods→ 4 = len(data[0][1200, 1350, 1500, 1400])37for col in range(num_periods):
    output
    === Per Period ===
  5. col_total ← 4500

    pass 1 of 4
    36num_periods = len(data[0])37for col0 in range(num_periods4):38    col_total→ 4500 = sum(row[col]500 for row in data[[1200, 1350, 1500, 1400], [800, 950, 1100, 1050], [2000, 1800, 2200, 2100], [500, 600, 700, 650]])39    print(f"Period {col0 + 1}: {col_total4500}")
    outputPeriod 1: 4500
    All 4 passes — pass 1 is the card above
    passcolrow[col]col_total
    105004500
    216004700
    327005500
    436505200
  6. main()

    41main()

Use nested loops or nested comprehensions to visit every cell.

row-major Process row by row: `for row in grid: for val in row:`

Find a value's position

Search for a value and return its row/column.

target_seat
find_position.py
Replay: real traced execution (multi-file project)
def main():
    # Movie theater seat map with seat IDs
    seat_map = [
        ["A1", "A2", "A3", "A4", "A5"],
        ["B1", "B2", "B3", "B4", "B5"],
        ["C1", "C2", "C3", "C4", "C5"],
        ["D1", "D2", "D3", "D4", "D5"]
    ]

    # Find this seat
    target_seat = "C3"

    # Search for the seat
    found_row = -1
    found_col = -1

    for row_idx, row in enumerate(seat_map):
        if target_seat in row:
            found_row = row_idx
            found_col = row.index(target_seat)
            break

    print("=== Seat Finder ===")
    print(f"Looking for seat: {target_seat}")

    if found_row != -1:
        print("Found at position:")
        print(f"  Row index: {found_row} (Row {found_row + 1})")
        print(f"  Col index: {found_col} (Seat {found_col + 1})")

        print(f"\nDirections: Walk to row {found_row + 1}, " +
              f"then count {found_col + 1} seat(s) from the left.")
    else:
        print("Seat not found!")

    # Show the map with target highlighted
    print("\n=== Seat Map ===")
    for row_idx, row in enumerate(seat_map):
        for col_idx, seat in enumerate(row):
            if row_idx == found_row and col_idx == found_col:
                print(f"[{seat}]", end="\t")
            else:
                print(f" {seat} ", end="\t")
        print()

main()
def main():
    # Movie theater seat map with seat IDs
    seat_map = [
        ["A1", "A2", "A3", "A4", "A5"],
        ["B1", "B2", "B3", "B4", "B5"],
        ["C1", "C2", "C3", "C4", "C5"],
        ["D1", "D2", "D3", "D4", "D5"]
    ]

    # Find this seat
    target_seat = "Z9"

    # Search for the seat
    found_row = -1
    found_col = -1

    for row_idx, row in enumerate(seat_map):
        if target_seat in row:
            found_row = row_idx
            found_col = row.index(target_seat)
            break

    print("=== Seat Finder ===")
    print(f"Looking for seat: {target_seat}")

    if found_row != -1:
        print("Found at position:")
        print(f"  Row index: {found_row} (Row {found_row + 1})")
        print(f"  Col index: {found_col} (Seat {found_col + 1})")

        print(f"\nDirections: Walk to row {found_row + 1}, " +
              f"then count {found_col + 1} seat(s) from the left.")
    else:
        print("Seat not found!")

    # Show the map with target highlighted
    print("\n=== Seat Map ===")
    for row_idx, row in enumerate(seat_map):
        for col_idx, seat in enumerate(row):
            if row_idx == found_row and col_idx == found_col:
                print(f"[{seat}]", end="\t")
            else:
                print(f" {seat} ", end="\t")
        print()

main()
  1. main()

    47main()48#@help found
  2. seat_map ← [['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']]

    1#@var=default,notfound2def main():3    # Movie theater seat map with seat IDs4    seat_map→ [['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']] = [5        ["A1", "A2", "A3", "A4", "A5"],6        ["B1", "B2", "B3", "B4", "B5"],7        ["C1", "C2", "C3", "C4", "C5"],8        ["D1", "D2", "D3", "D4", "D5"]9    ]10    11    # Find this seat12    target_seat→ C3 = "C3"  #@var=_,Z913    14    # Search for the seat  #?found15    found_row→ -1 = -116    found_col→ -1 = -1
  3. for row_idx, row in enumerate(seat_map):

    pass 1 of 3
    18for row_idx0, row['A1', 'A2', 'A3', 'A4', 'A5'] in enumerate(seat_map[['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']]):19    if target_seat in row:20        found_row = row_idx
    All 3 passes — pass 1 is the card above
    passrow_idxrowtarget_seatfound_rowfound_col
    10['A1', 'A2', 'A3', 'A4', 'A5']
    21['B1', 'B2', 'B3', 'B4', 'B5']
    32['C1', 'C2', 'C3', 'C4', 'C5']C322
  4. found_row ← 2, found_col ← 2

    18for row_idx, row in enumerate(seat_map):19    if target_seatC3 in row['C1', 'C2', 'C3', 'C4', 'C5']:20        found_row→ 2 = row_idx221        found_col→ 2 = row['C1', 'C2', 'C3', 'C4', 'C5'].index(target_seatC3)22        break
  5. print(f"Looking for seat: {target_seat}")

    24print("=== Seat Finder ===")25print(f"Looking for seat: {target_seatC3}")
    output=== Seat Finder ===
    Looking for seat: C3
  6. if found_row != -1:

    27if found_row2 != -1:28    print("Found at position:")29    print(f"  Row index: {found_row2} (Row {found_row + 1})")30    print(f"  Col index: {found_col2} (Seat {found_col + 1})")31    32    print(f"\nDirections: Walk to row {found_row2 + 1}, " +33          f"then count {found_col2 + 1} seat(s) from the left.")34else:
    outputFound at position:
      Row index: 2 (Row 3)
      Col index: 2 (Seat 3)
    
    Directions: Walk to row 3, then count 3 seat(s) from the left.
  7. print(" === Seat Map ===")

    37# Show the map with target highlighted38print("\n=== Seat Map ===")39for row_idx, row in enumerate(seat_map):
    output
    === Seat Map ===
  8. for row_idx, row in enumerate(seat_map):

    pass 1 of 4
    38print("\n=== Seat Map ===")39for row_idx0, row['A1', 'A2', 'A3', 'A4', 'A5'] in enumerate(seat_map[['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']]):40    for col_idx, seat in enumerate(row):41        if row_idx == found_row and col_idx == found_col:
    All 4 passes — pass 1 is the card above
    passrow_idxrowfound_rowcol_idxfound_colseat
    10['A1', 'A2', 'A3', 'A4', 'A5']
    21['B1', 'B2', 'B3', 'B4', 'B5']
    32['C1', 'C2', 'C3', 'C4', 'C5']222C3
    43['D1', 'D2', 'D3', 'D4', 'D5']
  9. for col_idx, seat in enumerate(row):

    pass 1 of 20
    39for row_idx, row in enumerate(seat_map):40    for col_idx0, seatA1 in enumerate(row['A1', 'A2', 'A3', 'A4', 'A5']):41        if row_idx == found_row and col_idx == found_col:42            print(f"[{seat}]", end="\t")
    20 passes — pass 1 is the card above
    passcol_idxseatrowrow_idxfound_rowfound_col
    10A1['A1', 'A2', 'A3', 'A4', 'A5']
    21A2['A1', 'A2', 'A3', 'A4', 'A5']
    32A3['A1', 'A2', 'A3', 'A4', 'A5']
    43A4['A1', 'A2', 'A3', 'A4', 'A5']
    54A5['A1', 'A2', 'A3', 'A4', 'A5']
    60B1['B1', 'B2', 'B3', 'B4', 'B5']
    71B2['B1', 'B2', 'B3', 'B4', 'B5']
    82B3['B1', 'B2', 'B3', 'B4', 'B5']
    93B4['B1', 'B2', 'B3', 'B4', 'B5']
    ⋯ 9 more passes ⋯
    193D4['D1', 'D2', 'D3', 'D4', 'D5']
    204D5['D1', 'D2', 'D3', 'D4', 'D5']
  10. else:

    pass 1 of 19
    41    if row_idx == found_row and col_idx == found_col:42        print(f"[{seat}]", end="\t")43    else:44        print(f" {seatA1} ", end="\t")45print()
    output A1 
    19 passes — pass 1 is the card above
    passseatrow_idxfound_rowcol_idxfound_col
    1A1
    2A2
    3A3
    4A4
    5A5
    6B1
    7B2
    8B3
    9B4
    ⋯ 8 more passes ⋯
    18D4
    19D5
  11. print()

    44        print(f" {seat} ", end="\t")45print()
  12. print()

    44        print(f" {seat} ", end="\t")45print()
  13. if row_idx == found_row and col_idx == found_col:

    40for col_idx, seat in enumerate(row):41    if row_idx2 == found_row2 and col_idx2 == found_col2:42        print(f"[{seatC3}]", end="\t")43    else:
    output[C3]
  14. print()

    44        print(f" {seat} ", end="\t")45print()
  15. print()

    44        print(f" {seat} ", end="\t")45print()
  16. main()

    47main()48#@help found
  1. main()

    46main()
  2. seat_map ← [['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']]

    1def main():2    # Movie theater seat map with seat IDs3    seat_map→ [['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']] = [4        ["A1", "A2", "A3", "A4", "A5"],5        ["B1", "B2", "B3", "B4", "B5"],6        ["C1", "C2", "C3", "C4", "C5"],7        ["D1", "D2", "D3", "D4", "D5"]8    ]9    10    # Find this seat11    target_seat→ Z9 = "Z9"12    13    # Search for the seat14    found_row→ -1 = -115    found_col→ -1 = -1
  3. for row_idx, row in enumerate(seat_map):

    pass 1 of 4
    17for row_idx0, row['A1', 'A2', 'A3', 'A4', 'A5'] in enumerate(seat_map[['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']]):18    if target_seat in row:19        found_row = row_idx
    All 4 passes — pass 1 is the card above
    passrow_idxrow
    10['A1', 'A2', 'A3', 'A4', 'A5']
    21['B1', 'B2', 'B3', 'B4', 'B5']
    32['C1', 'C2', 'C3', 'C4', 'C5']
    43['D1', 'D2', 'D3', 'D4', 'D5']
  4. print(f"Looking for seat: {target_seat}")

    23print("=== Seat Finder ===")24print(f"Looking for seat: {target_seatZ9}")
    output=== Seat Finder ===
    Looking for seat: Z9
  5. else:

    31    print(f"\nDirections: Walk to row {found_row + 1}, " +32          f"then count {found_col + 1} seat(s) from the left.")33else:34    print("Seat not found!")
    outputSeat not found!
  6. print(" === Seat Map ===")

    36# Show the map with target highlighted37print("\n=== Seat Map ===")38for row_idx, row in enumerate(seat_map):
    output
    === Seat Map ===
  7. for row_idx, row in enumerate(seat_map):

    pass 1 of 4
    37print("\n=== Seat Map ===")38for row_idx0, row['A1', 'A2', 'A3', 'A4', 'A5'] in enumerate(seat_map[['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5'], ['C1', 'C2', 'C3', 'C4', 'C5'], ['D1', 'D2', 'D3', 'D4', 'D5']]):39    for col_idx, seat in enumerate(row):40        if row_idx == found_row and col_idx == found_col:
    All 4 passes — pass 1 is the card above
    passrow_idxrow
    10['A1', 'A2', 'A3', 'A4', 'A5']
    21['B1', 'B2', 'B3', 'B4', 'B5']
    32['C1', 'C2', 'C3', 'C4', 'C5']
    43['D1', 'D2', 'D3', 'D4', 'D5']
  8. for col_idx, seat in enumerate(row):

    pass 1 of 20
    38for row_idx, row in enumerate(seat_map):39    for col_idx0, seatA1 in enumerate(row['A1', 'A2', 'A3', 'A4', 'A5']):40        if row_idx == found_row and col_idx == found_col:41            print(f"[{seat}]", end="\t")
    20 passes — pass 1 is the card above
    passcol_idxseatrow
    10A1['A1', 'A2', 'A3', 'A4', 'A5']
    21A2['A1', 'A2', 'A3', 'A4', 'A5']
    32A3['A1', 'A2', 'A3', 'A4', 'A5']
    43A4['A1', 'A2', 'A3', 'A4', 'A5']
    54A5['A1', 'A2', 'A3', 'A4', 'A5']
    60B1['B1', 'B2', 'B3', 'B4', 'B5']
    71B2['B1', 'B2', 'B3', 'B4', 'B5']
    82B3['B1', 'B2', 'B3', 'B4', 'B5']
    93B4['B1', 'B2', 'B3', 'B4', 'B5']
    ⋯ 9 more passes ⋯
    193D4['D1', 'D2', 'D3', 'D4', 'D5']
    204D5['D1', 'D2', 'D3', 'D4', 'D5']
  9. else:

    pass 1 of 20
    40    if row_idx == found_row and col_idx == found_col:41        print(f"[{seat}]", end="\t")42    else:43        print(f" {seatA1} ", end="\t")44print()
    output A1 
    20 passes — pass 1 is the card above
    passseat
    1A1
    2A2
    3A3
    4A4
    5A5
    6B1
    7B2
    8B3
    9B4
    ⋯ 9 more passes ⋯
    19D4
    20D5
  10. print()

    43        print(f" {seat} ", end="\t")44print()
  11. print()

    43        print(f" {seat} ", end="\t")44print()
  12. print()

    43        print(f" {seat} ", end="\t")44print()
  13. print()

    43        print(f" {seat} ", end="\t")44print()
  14. main()

    46main()

Return early when found. Return None or (-1, -1) if not found.

Exercise: comprehension.py

Create 2D lists with list comprehensions