Collections
2D Lists
Grids and Tables
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.
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()
main()
28main()29#@help seatsrows ← 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):for row in range(rows):
pass 1 of 311seat_number = 112for row0 in range(rows3):13 for col in range(cols):14 seating[row][col] = seat_numberAll 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 seating[row][col] ← 1, seat_number ← 2
pass 1 of 1212for row in range(rows):13 for col0 in range(cols4):14 seating[row][col]→ 1 = seat_number115 seat_number→ 2 += 1All 12 passes — pass 1 is the card above pass colseating[row][col]seat_number1 0 1 1 → 2 2 1 2 2 → 3 3 2 3 3 → 4 4 3 4 4 → 5 5 0 5 5 → 6 6 1 6 6 → 7 7 2 7 7 → 8 8 3 8 8 → 9 9 0 9 9 → 10 10 1 10 10 → 11 11 2 11 11 → 12 12 3 12 12 → 13 print("=== Theater Seating Chart ===")
17# Display the seating chart18print("=== Theater Seating Chart ===")19for row_idx, row in enumerate(seating):output=== Theater Seating Chart ===for row_idx, row in enumerate(seating):
pass 1 of 318print("=== 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 pass row_idxrow1 0 [1, 2, 3, 4] 2 1 [5, 6, 7, 8] 3 2 [9, 10, 11, 12] for seat in row:
pass 1 of 1220print(f"Row {row_idx + 1}: ", end="")21for seat1 in row[1, 2, 3, 4]:22 print(f"{seat1}\t", end="")23print()output1All 12 passes — pass 1 is the card above pass seatrow1 1 [1, 2, 3, 4] 2 2 [1, 2, 3, 4] 3 3 [1, 2, 3, 4] 4 4 [1, 2, 3, 4] 5 5 [5, 6, 7, 8] 6 6 [5, 6, 7, 8] 7 7 [5, 6, 7, 8] 8 8 [5, 6, 7, 8] 9 9 [9, 10, 11, 12] 10 10 [9, 10, 11, 12] 11 11 [9, 10, 11, 12] 12 12 [9, 10, 11, 12] print()
22 print(f"{seat}\t", end="")23print()print()
22 print(f"{seat}\t", end="")23print()print()
22 print(f"{seat}\t", end="")23print()total_seats ← 12
25total_seats→ 12 = rows3 * cols426print(f"Total seats: {total_seats12}")outputTotal seats: 12main()
28main()29#@help seats
main()
27main()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):for row in range(rows):
pass 1 of 510seat_number = 111for row0 in range(rows5):12 for col in range(cols):13 seating[row][col] = seat_numberAll 5 passes — pass 1 is the card above pass row1 0 2 1 3 2 4 3 5 4 seating[row][col] ← 1, seat_number ← 2
pass 1 of 2011for row in range(rows):12 for col0 in range(cols4):13 seating[row][col]→ 1 = seat_number114 seat_number→ 2 += 120 passes — pass 1 is the card above pass colseating[row][col]seat_number1 0 1 1 → 2 2 1 2 2 → 3 3 2 3 3 → 4 4 3 4 4 → 5 5 0 5 5 → 6 6 1 6 6 → 7 7 2 7 7 → 8 8 3 8 8 → 9 9 0 9 9 → 10 ⋯ 9 more passes ⋯ 19 2 19 19 → 20 20 3 20 20 → 21 print("=== Theater Seating Chart ===")
16# Display the seating chart17print("=== Theater Seating Chart ===")18for row_idx, row in enumerate(seating):output=== Theater Seating Chart ===for row_idx, row in enumerate(seating):
pass 1 of 517print("=== 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 pass row_idxrow1 0 [1, 2, 3, 4] 2 1 [5, 6, 7, 8] 3 2 [9, 10, 11, 12] 4 3 [13, 14, 15, 16] 5 4 [17, 18, 19, 20] for seat in row:
pass 1 of 2019print(f"Row {row_idx + 1}: ", end="")20for seat1 in row[1, 2, 3, 4]:21 print(f"{seat1}\t", end="")22print()output120 passes — pass 1 is the card above pass seatrow1 1 [1, 2, 3, 4] 2 2 [1, 2, 3, 4] 3 3 [1, 2, 3, 4] 4 4 [1, 2, 3, 4] 5 5 [5, 6, 7, 8] 6 6 [5, 6, 7, 8] 7 7 [5, 6, 7, 8] 8 8 [5, 6, 7, 8] 9 9 [9, 10, 11, 12] ⋯ 9 more passes ⋯ 19 19 [17, 18, 19, 20] 20 20 [17, 18, 19, 20] print()
21 print(f"{seat}\t", end="")22print()print()
21 print(f"{seat}\t", end="")22print()print()
21 print(f"{seat}\t", end="")22print()print()
21 print(f"{seat}\t", end="")22print()print()
21 print(f"{seat}\t", end="")22print()total_seats ← 20
24total_seats→ 20 = rows5 * cols425print(f"Total seats: {total_seats20}")outputTotal seats: 20main()
27main()
main()
27main()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):for row in range(rows):
pass 1 of 310seat_number = 111for row0 in range(rows3):12 for col in range(cols):13 seating[row][col] = seat_numberAll 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 seating[row][col] ← 1, seat_number ← 2
pass 1 of 1811for row in range(rows):12 for col0 in range(cols6):13 seating[row][col]→ 1 = seat_number114 seat_number→ 2 += 118 passes — pass 1 is the card above pass colseating[row][col]seat_number1 0 1 1 → 2 2 1 2 2 → 3 3 2 3 3 → 4 4 3 4 4 → 5 5 4 5 5 → 6 6 5 6 6 → 7 7 0 7 7 → 8 8 1 8 8 → 9 9 2 9 9 → 10 ⋯ 7 more passes ⋯ 17 4 17 17 → 18 18 5 18 18 → 19 print("=== Theater Seating Chart ===")
16# Display the seating chart17print("=== Theater Seating Chart ===")18for row_idx, row in enumerate(seating):output=== Theater Seating Chart ===for row_idx, row in enumerate(seating):
pass 1 of 317print("=== 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 pass row_idxrow1 0 [1, 2, 3, 4, 5, 6] 2 1 [7, 8, 9, 10, 11, 12] 3 2 [13, 14, 15, 16, 17, 18] for seat in row:
pass 1 of 1819print(f"Row {row_idx + 1}: ", end="")20for seat1 in row[1, 2, 3, 4, 5, 6]:21 print(f"{seat1}\t", end="")22print()output118 passes — pass 1 is the card above pass seatrow1 1 [1, 2, 3, 4, 5, 6] 2 2 [1, 2, 3, 4, 5, 6] 3 3 [1, 2, 3, 4, 5, 6] 4 4 [1, 2, 3, 4, 5, 6] 5 5 [1, 2, 3, 4, 5, 6] 6 6 [1, 2, 3, 4, 5, 6] 7 7 [7, 8, 9, 10, 11, 12] 8 8 [7, 8, 9, 10, 11, 12] 9 9 [7, 8, 9, 10, 11, 12] ⋯ 7 more passes ⋯ 17 17 [13, 14, 15, 16, 17, 18] 18 18 [13, 14, 15, 16, 17, 18] print()
21 print(f"{seat}\t", end="")22print()print()
21 print(f"{seat}\t", end="")22print()print()
21 print(f"{seat}\t", end="")22print()total_seats ← 18
24total_seats→ 18 = rows3 * cols625print(f"Total seats: {total_seats18}")outputTotal seats: 18main()
27main()
A 2D list is a list of lists. Use list comprehension to create grids.
Access a specific seat
Look up a value using row and column indices.
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()
main()
35main()36#@help rowcolprices ← [[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 Frirow_str ← $50 $50 $55 $55 $75
pass 1 of 331for 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 $75All 3 passes — pass 1 is the card above pass rrowsections[r]row_str1 0 [50, 50, 55, 55, 75] Front $50 $50 $55 $55 $75 2 1 [40, 40, 45, 45, 60] Middle $40 $40 $45 $45 $60 3 2 [25, 25, 30, 30, 45] Back $25 $25 $30 $30 $45 main()
35main()36#@help rowcol
main()
34main()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 Frirow_str ← $50 $50 $55 $55 $75
pass 1 of 330for 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 $75All 3 passes — pass 1 is the card above pass rrowsections[r]row_str1 0 [50, 50, 55, 55, 75] Front $50 $50 $55 $55 $75 2 1 [40, 40, 45, 45, 60] Middle $40 $40 $45 $45 $60 3 2 [25, 25, 30, 30, 45] Back $25 $25 $30 $30 $45 main()
34main()
main()
34main()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 Frirow_str ← $50 $50 $55 $55 $75
pass 1 of 330for 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 $75All 3 passes — pass 1 is the card above pass rrowsections[r]row_str1 0 [50, 50, 55, 55, 75] Front $50 $50 $55 $55 $75 2 1 [40, 40, 45, 45, 60] Middle $40 $40 $45 $45 $60 3 2 [25, 25, 30, 30, 45] Back $25 $25 $30 $30 $45 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.
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()
main()
40main()41#@help updateseats ← [[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 ===def print_seating(seats):
pass 1 of 334def 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 4All 3 passes — pass 1 is the card above pass seats1 [[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]] symbols ← ['O', 'O', 'O', 'O']
pass 1 of 935print(" 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 OAll 9 passes — pass 1 is the card above pass rrowseatsreserve_rowreserve_colsymbolsseats[reserve_row][reserve_col]seats[0][0]seats[2][3]available1 0 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 2 1 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 3 2 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] 1 2 ['O', 'O', 'O', 'O'] 1 — — — 4 0 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 5 1 [0, 0, 1, 0] [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]] — — ['O', 'O', 'X', 'O'] — — — — 6 2 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — 1 1 — 7 0 [1, 0, 0, 0] [[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] — — ['X', 'O', 'O', 'O'] — — — — 8 1 [0, 0, 1, 0] [[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] — — ['O', 'O', 'X', 'O'] — — — — 9 2 [0, 0, 0, 1] [[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] — — ['O', 'O', 'O', 'X'] — — — 9 main()
40main()41#@help update
main()
39main()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 ===def print_seating(seats):
pass 1 of 333def 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 4All 3 passes — pass 1 is the card above pass seats1 [[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]] symbols ← ['O', 'O', 'O', 'O']
pass 1 of 934print(" 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 OAll 9 passes — pass 1 is the card above pass rrowseatsreserve_rowreserve_colsymbolsseats[reserve_row][reserve_col]seats[0][0]seats[2][3]available1 0 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 2 1 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 3 2 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] 0 2 ['O', 'O', 'O', 'O'] 1 — — — 4 0 [0, 0, 1, 0] [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'X', 'O'] — — — — 5 1 [0, 0, 0, 0] [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 6 2 [0, 0, 0, 0] [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — 1 1 — 7 0 [1, 0, 1, 0] [[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]] — — ['X', 'O', 'X', 'O'] — — — — 8 1 [0, 0, 0, 0] [[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]] — — ['O', 'O', 'O', 'O'] — — — — 9 2 [0, 0, 0, 1] [[1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]] — — ['O', 'O', 'O', 'X'] — — — 9 main()
39main()
main()
39main()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 ===def print_seating(seats):
pass 1 of 333def 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 4All 3 passes — pass 1 is the card above pass seats1 [[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]] symbols ← ['O', 'O', 'O', 'O']
pass 1 of 934print(" 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 OAll 9 passes — pass 1 is the card above pass rrowseatsreserve_rowreserve_colsymbolsseats[reserve_row][reserve_col]seats[0][0]seats[2][3]available1 0 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 2 1 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 3 2 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] 1 1 ['O', 'O', 'O', 'O'] 1 — — — 4 0 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — — — — 5 1 [0, 1, 0, 0] [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]] — — ['O', 'X', 'O', 'O'] — — — — 6 2 [0, 0, 0, 0] [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]] — — ['O', 'O', 'O', 'O'] — 1 1 — 7 0 [1, 0, 0, 0] [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]] — — ['X', 'O', 'O', 'O'] — — — — 8 1 [0, 1, 0, 0] [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]] — — ['O', 'X', 'O', 'O'] — — — — 9 2 [0, 0, 0, 1] [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]] — — ['O', 'O', 'O', 'X'] — — — 9 main()
39main()
Assign directly: grid[row][col] = value. Lists are mutable.
Sum all values
Calculate a total by traversing every cell.
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()
main()
42main()43#@help sumsales ← [[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 ===row_total ← 405
pass 1 of 330products = ["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: 405All 3 passes — pass 1 is the card above pass irowproducts[i]row_total1 0 [120, 135, 150] Widgets 405 2 1 [80, 95, 110] Gadgets 285 3 2 [200, 180, 220] Gizmos 600 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 ===col_total ← 400
pass 1 of 337num_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: 400All 3 passes — pass 1 is the card above pass colrow[col]col_total1 0 200 400 2 1 180 410 3 2 220 480 main()
42main()43#@help sum
main()
41main()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 ===row_total ← 5450
pass 1 of 429products = ["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: 5450All 4 passes — pass 1 is the card above pass irowproducts[i]row_total1 0 [1200, 1350, 1500, 1400] Widgets 5450 2 1 [800, 950, 1100, 1050] Gadgets 3900 3 2 [2000, 1800, 2200, 2100] Gizmos 8100 4 3 [500, 600, 700, 650] Sprockets 2450 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 ===col_total ← 4500
pass 1 of 436num_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: 4500All 4 passes — pass 1 is the card above pass colrow[col]col_total1 0 500 4500 2 1 600 4700 3 2 700 5500 4 3 650 5200 main()
41main()
Use nested loops or nested comprehensions to visit every cell.
Find a value's position
Search for a value and return its row/column.
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()
main()
47main()48#@help foundseat_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 = -1for row_idx, row in enumerate(seat_map):
pass 1 of 318for 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_idxAll 3 passes — pass 1 is the card above pass row_idxrowtarget_seatfound_rowfound_col1 0 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — 2 1 ['B1', 'B2', 'B3', 'B4', 'B5'] — — — 3 2 ['C1', 'C2', 'C3', 'C4', 'C5'] C3 2 2 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 breakprint(f"Looking for seat: {target_seat}")
24print("=== Seat Finder ===")25print(f"Looking for seat: {target_seatC3}")output=== Seat Finder === Looking for seat: C3if 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.print(" === Seat Map ===")
37# Show the map with target highlighted38print("\n=== Seat Map ===")39for row_idx, row in enumerate(seat_map):output === Seat Map ===for row_idx, row in enumerate(seat_map):
pass 1 of 438print("\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 pass row_idxrowfound_rowcol_idxfound_colseat1 0 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — — 2 1 ['B1', 'B2', 'B3', 'B4', 'B5'] — — — — 3 2 ['C1', 'C2', 'C3', 'C4', 'C5'] 2 2 2 C3 4 3 ['D1', 'D2', 'D3', 'D4', 'D5'] — — — — for col_idx, seat in enumerate(row):
pass 1 of 2039for 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 pass col_idxseatrowrow_idxfound_rowfound_col1 0 A1 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — 2 1 A2 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — 3 2 A3 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — 4 3 A4 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — 5 4 A5 ['A1', 'A2', 'A3', 'A4', 'A5'] — — — 6 0 B1 ['B1', 'B2', 'B3', 'B4', 'B5'] — — — 7 1 B2 ['B1', 'B2', 'B3', 'B4', 'B5'] — — — 8 2 B3 ['B1', 'B2', 'B3', 'B4', 'B5'] — — — 9 3 B4 ['B1', 'B2', 'B3', 'B4', 'B5'] — — — ⋯ 9 more passes ⋯ 19 3 D4 ['D1', 'D2', 'D3', 'D4', 'D5'] — — — 20 4 D5 ['D1', 'D2', 'D3', 'D4', 'D5'] — — — else:
pass 1 of 1941 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 A119 passes — pass 1 is the card above pass seatrow_idxfound_rowcol_idxfound_col1 A1 — — — — 2 A2 — — — — 3 A3 — — — — 4 A4 — — — — 5 A5 — — — — 6 B1 — — — — 7 B2 — — — — 8 B3 — — — — 9 B4 — — — — ⋯ 8 more passes ⋯ 18 D4 — — — — 19 D5 — — — — print()
44 print(f" {seat} ", end="\t")45print()print()
44 print(f" {seat} ", end="\t")45print()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]print()
44 print(f" {seat} ", end="\t")45print()print()
44 print(f" {seat} ", end="\t")45print()main()
47main()48#@help found
main()
46main()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 = -1for row_idx, row in enumerate(seat_map):
pass 1 of 417for 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_idxAll 4 passes — pass 1 is the card above pass row_idxrow1 0 ['A1', 'A2', 'A3', 'A4', 'A5'] 2 1 ['B1', 'B2', 'B3', 'B4', 'B5'] 3 2 ['C1', 'C2', 'C3', 'C4', 'C5'] 4 3 ['D1', 'D2', 'D3', 'D4', 'D5'] print(f"Looking for seat: {target_seat}")
23print("=== Seat Finder ===")24print(f"Looking for seat: {target_seatZ9}")output=== Seat Finder === Looking for seat: Z9else:
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!print(" === Seat Map ===")
36# Show the map with target highlighted37print("\n=== Seat Map ===")38for row_idx, row in enumerate(seat_map):output === Seat Map ===for row_idx, row in enumerate(seat_map):
pass 1 of 437print("\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 pass row_idxrow1 0 ['A1', 'A2', 'A3', 'A4', 'A5'] 2 1 ['B1', 'B2', 'B3', 'B4', 'B5'] 3 2 ['C1', 'C2', 'C3', 'C4', 'C5'] 4 3 ['D1', 'D2', 'D3', 'D4', 'D5'] for col_idx, seat in enumerate(row):
pass 1 of 2038for 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 pass col_idxseatrow1 0 A1 ['A1', 'A2', 'A3', 'A4', 'A5'] 2 1 A2 ['A1', 'A2', 'A3', 'A4', 'A5'] 3 2 A3 ['A1', 'A2', 'A3', 'A4', 'A5'] 4 3 A4 ['A1', 'A2', 'A3', 'A4', 'A5'] 5 4 A5 ['A1', 'A2', 'A3', 'A4', 'A5'] 6 0 B1 ['B1', 'B2', 'B3', 'B4', 'B5'] 7 1 B2 ['B1', 'B2', 'B3', 'B4', 'B5'] 8 2 B3 ['B1', 'B2', 'B3', 'B4', 'B5'] 9 3 B4 ['B1', 'B2', 'B3', 'B4', 'B5'] ⋯ 9 more passes ⋯ 19 3 D4 ['D1', 'D2', 'D3', 'D4', 'D5'] 20 4 D5 ['D1', 'D2', 'D3', 'D4', 'D5'] else:
pass 1 of 2040 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 A120 passes — pass 1 is the card above pass seat1 A1 2 A2 3 A3 4 A4 5 A5 6 B1 7 B2 8 B3 9 B4 ⋯ 9 more passes ⋯ 19 D4 20 D5 print()
43 print(f" {seat} ", end="\t")44print()print()
43 print(f" {seat} ", end="\t")44print()print()
43 print(f" {seat} ", end="\t")44print()print()
43 print(f" {seat} ", end="\t")44print()main()
46main()
Return early when found. Return None or (-1, -1) if not found.
Exercise: comprehension.py
Create 2D lists with list comprehensions