0

I have a list: the_board = [['','',''], ['','',''], ['','','']].

It will be updated with X's and O's, but I need to make a list which contains a tuple of the indexes that don't have an X or an O.

For example, if the list had an 'X' at index the_board[1][2], the new list should be like: free_fields = [(0,0), (0,1), (0,2), (1,0), (1,1), (2,0), (2,1), (2,2) #note how index [1][2] is not a tuple in the new list.

How would I go about doing this? Thanks.

Boinbo
  • 9
  • 3

4 Answers4

0

you can doit with simple nested for loops, the first for iterating over the list and the inner loop for iterating over the tuples:

for each_tuple in free_fields:
  for each_element in each_tuple:
     #do some work

Now if you want a code that generates tuples inside a list free_fields:

x = 10 #tuples
free_fields = []
while x <=10:
  free_fields.append(tuple(var_x,var_y))
  x += 1

if you want to remove some pair from the list, use list.remove() method:

free_fields.remove((left_val,right_val)) 
Adam
  • 2,820
  • 1
  • 13
  • 33
0
x = 1
y = 2
free_fields = [(0,0), (0,1), (0,2), (1,0), (1,1), (1, 2), (2,0), (2,1), (2,2)]
the_board = [['','',''], ['','',''], ['','','']]
the_board[x][y] = "X"
print(the_board)
# [['', '', ''], ['', '', 'X'], ['', '', '']]

free_fields.remove((x, y))
print(free_fields)
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2)]

Remove it from the list using .remove().

You can put this code in a loop for more values of x and y.

Note: Do not remove from free_fields while iterating on it. Ideally, you should be iterating on values of x and y and not on the board and free_fields.

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

Usually in python if you need both the index and the item itself you'll use enumerate. In this case you want to have the index to return it, but you also need the content of the rows/cell to inspect the content (check it's empty).

A nice tool you could use to make your code simpler is too use generators (yield form). In this case that will save you the burden of creating a list, adding empty cells indexes and then return it at the end. Instead you'll yield every empty cell indexes.

Putting all these together gives:

def free_fields(board):
    for i, row in enumerate(board):
        for j, cell in enumerate(row):
            if not cell:
                yield i, j
            
fields = list(free_fields(the_board))

Notice how free_fields returns an iterator so we may need to convert it to a list. But if we only need to iterate over empty cells we could simply write:

for i, j in free_fields(the_board):
    print(i, j)

Doing this also allows to save the memory we should have used to store the list of indexes.

cglacet
  • 8,873
  • 4
  • 45
  • 60
0

To get the actual indexes, iterate by index instead of by item.

    free_fields = [ ] 
    for i in range(3):
        for j in range:
            if the_board[i][j] = '' :
                free_fields.append(tuple([i,j])
Vlad Zhdanov
  • 65
  • 1
  • 6