Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

I am trying to create my tic-tac-toe game in python. One part requires me to find any differences between my two lists, as seen in this code below. I need it to find my location of the change and a letter located there.

The 2 lists that I want to differentiate are:

ttt_board = [[' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]

ttt_newboard = [[' ', 'O', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]

For Example, 

return ttt_newboard[0][2]. 

However I wouldn't know the index, I would only be provided a list. 

1 Answer

0 votes
by (36.8k points)

Try this:

ttt_board = [[' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]

ttt_newboard = [[' ', 'O', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]

def find(old, new):

    for i in range(len(old)):

        for j in range(len(old[i])):

            if (old[i][j] != new[i][j]):

                return (i, j, new[i][j])

print(find(ttt_board, ttt_newboard))

Output:

(0, 1, 'O')

Want to gain end-to-end skills in Data Science with Python? Enroll today in this Python for Data Science Course and be a master in it 

Browse Categories

...