Faster track to recognizing patterns in looping through nested sequences I frequently have patterns as below, usually not this simple, which require me to first code a working solution for a single test case, and then to refactor the code to make it more concise. See the first incarnation below, then the final code. I'm looking to see what other techniques some use to skip the initial step, other than suddenly being able to see the solution with experience. Or if you would approach the example below in an entirely different fashion.

Initial code:

# 4.) A Tic-tac-toe game (see http://en.wikipedia.org/wiki/Tic-tac-toe) can be
# simulated by a computer using a 3 x 3 matrix of strings. Each string has one 
# of three possible values, 'X', 'O' or '' (an empty block). Moreover, 
# individual move of a player can be simulated with a tuple, containing three 
# values: the x-coordinate (0..2), the y-coordinate (0..2) and a string 
# containing the mark of the player ('X' or 'O'). Write a procedure 
# insertMoves(board, listOfMoves), which has two parameters: a 3 x 3 matrix 
# board and a list of players moves as tuples. The procedure then inserts all 
# moves in a list to the game board.
def insertMoves(board, list_of_moves):
    for move in list_of_moves:
        if move[0] == 0:
            if move[1] == 0:
                (board[0])[0] = move[2]
            if move[1] == 1:
                (board[1])[0] = move[2]
            if move[1] == 2:
                (board[2])[0] = move[2]
        elif move[0] == 1:
            if move[1] == 0:
                (board[0])[1] = move[2]                
            if move[1] == 1:
                (board[1])[1] = move[2]
            if move[1] == 2:
                (board[2])[1] = move[2]
        elif move[0] == 2:
            if move[1] == 0:
                (board[0])[2] = move[2]                
            if move[1] == 1:
                (board[1])[2] = move[2]
            if move[1] == 2:
                (board[2])[2] = move[2] 
    return board   

row = [''] * 3
board = [row, row[:], row[:]]    
moves = [(0, 0, 'A'), (0, 1, 'B'), (0, 2, 'C'), (1, 0, 'D'), (1, 1, 'E'), 
         (1, 2, 'F'), (2, 0, 'G'), (2, 1, 'H'), (2, 2, 'I')]
final_board = insertMoves(board, moves)
for row in final_board:
    print row


And the final function definition:

def insertMoves(board, list_of_moves):
    for move in list_of_moves:
        for i in range(0, 3):
            if move[0] == i:
                for j in range(0, 3):
                    if move[1] == j:
                        (board[j])[i] = move[2]
    return board
I would write the loop like this:

for i, j, move in list_of_moves:
    assert(0 &lt;= i &lt; 3 and 0 &lt;= j &lt; 3 and move in 'XO')
    board[j][i] = move


or maybe:

VALID_MOVES = 'XO'
for i, j, move in list_of_moves:
    assert(0 &lt;= j &lt; len(board) and 0 &lt;= i &lt; len(board[j]) and move in VALID_MOVES)
    board[j][i] = move


depending on how general I needed the implementation to be.

Something that you might find helpful would be to step through your code in the Python debugger. Look at each line as it is executed and check that it makes sense. For example:

&gt;&gt;&gt; board = [[''] * 3 for _ in range(3)]
&gt;&gt;&gt; moves = [(1,1,'X'), (0,0,'O')]
&gt;&gt;&gt; import pdb
&gt;&gt;&gt; pdb.run("insertMoves(board, moves)")
&gt; &lt;string&gt;(1)&lt;module&gt;()
(Pdb) step
--Call--
&gt; cr32933.py(1)insertMoves()
-&gt; def insertMoves(board, list_of_moves):
(Pdb) step
&gt; cr32933.py(2)insertMoves()
-&gt; for move in list_of_moves:
(Pdb) step
&gt; cr32933.py(3)insertMoves()
-&gt; for i in range(0, 3):
(Pdb) print move
(1, 1, 'X')
(Pdb) step
&gt; cr32933.py(4)insertMoves()
-&gt; if move[0] == i:
(Pdb) print i
0
(Pdb) step
&gt; cr32933.py(3)insertMoves()
-&gt; for i in range(0, 3):
(Pdb) step
&gt; cr32933.py(4)insertMoves()
-&gt; if move[0] == i:
(Pdb) print i
1


At about this point (if not earlier) you should be saying to yourself, "why did I have to go round the loop in order to discover that move[0] has the value 1?"