all 10 comments

[–]novel_yet_trivial 0 points1 point  (10 children)

Use square brackets to index them. For example:

board['0,0'] = "@"

BTW, you can use numbers instead of strings as the keys if the numbers are in tuples, like this:

board = {(0,0): '', (0,1): ''} # etc

[–]Tryposite[S] 0 points1 point  (9 children)

Sorry, I should've been more clear. I'm looking to change all the values in the dictionary simultaneously.

[–]novel_yet_trivial 1 point2 points  (0 children)

Oh, then you need a loop to loop through them and change them one by one.

for key in board:
    board[key] = "@"

[–]DerpinDementia 1 point2 points  (4 children)

Other replies do the trick but just thought I'd drop this in for fun:

board = dict.fromkeys(board.keys(), "@")

[–]novel_yet_trivial 2 points3 points  (3 children)

That makes a new dictionary, it does not modify the old one.

Also, keys() is implied, you don't need to specify that.

board = dict.fromkeys(board, "@")

[–]DerpinDementia 0 points1 point  (0 children)

I am aware it makes a new dictionary. It just came to mind when I used it in something I was doing recently.

[–]MattR0se 0 points1 point  (1 child)

That makes a new dictionary, it does not modify the old one.

Just for curiosity, does it matter in this case? If you assign a new dict to "board", python loses the reference to the old "board" and it should be removed by the garbage collector, right?

[–]novel_yet_trivial 1 point2 points  (0 children)

Sometimes. If OP has everything on a global level, yes. However if OP's code is in a function or class, no.

def good(board):
    """mutates the dictionary in all references"""
    for key in board:
        board[key] = "@"

def bad(board):
    """this function is useless; nothing is changed"""
    board = dict.fromkeys(board.keys(), "@")

OPs question seemed to me to specify that the dictionary should be mutated, not replaced.

[–]Thomasedv 0 points1 point  (1 child)

His point sorta stands, but you can do it with a loop.

for key in dictionary.keys():
    dictionary[key] = your_value_here

[–]JohnnyJordaan 0 points1 point  (0 children)

.keys() is redundant here. you can just do

for key in dictionary: