-2

What do I need to make this work?

def get_column(game, col_num):

board = [['O', 'X', 'O'], ['X', ' ', ' '], ['X', ' ', ' ']] column1 = get_column(board, 0) print(column1)

and the result should be ['O', 'X,' 'X']

2 Answers2

2

Use a list comprehension to access the corresponding column in each row.

def get_column(board, col_num):
    return [ row[col_num] for row in board ]
Anson Miu
  • 1,171
  • 7
  • 6
0

Should look something like this:

def get_column(board, col_num):
    return [i[col_num] for i in board]

board = [['O', 'X', 'O'], ['X', ' ', ' '], ['X', ' ', ' ']] 
column1 = get_column(board, 0) 
print(column1)
ssnk001
  • 170
  • 5
  • 1
    I think OP represents board by a list of rows, so your solution is returning the nth row. – Anson Miu Apr 12 '21 at 22:45
  • IMO `i` is conventionally used to name loop indexes, so maybe it isn’t the best name choice for a list in a 2D list :) – Anson Miu Apr 12 '21 at 22:48