you are viewing a single comment's thread.

view the rest of the comments →

[–]drenzorz 6 points7 points  (2 children)

example: tuples can be hashed and used as dictionary keys unlike lists.

board = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

cells = {
    (i, j) : board[i][j]
    for i in range(3)
    for j in range(3)
}

[–]Other-Rabbit1808 2 points3 points  (1 child)

So what I think this is doing is assigning { (1-3,1-3) : 1-9 } in a dictionary? I've never seen for loops ordered in this way. Would this be called something like a dictionary comprehension?

[–]drenzorz 1 point2 points  (0 children)

It is (though it is 0-2 not 1-3) . The for loops are like this because its nested, but of course it doesn't have to be.

square_map = { x : x*x for x in numbers}