Home > OS >  How to assign values from one master dictionary to another?
How to assign values from one master dictionary to another?

Time:06-28

I'm currently solving a project in chapter 5 of the Automate the Boring stuff concerning a chess dictionary validator. I'm currently trying to create a dictionary from an existing master dictionary that contains the key, values of the board pieces in their respective colors and their quantity. Currently, the code I have is:

theBoard = {
"pieces": {"pawn": 8, "knight": 2, "bishop": 2, "rook": 2, "queen": 1, "king": 1},
"color": ["w", "b"],
"height": ["a", "b", "c", "d", "e", "f", "g", "h"],
"width": ['1', '2', '3', '4', '5', '6', '7', '8']}
tot_pieces = dict.fromkeys([''.join(t) for t in list(itertools.product(theBoard['color'], theBoard['pieces']))],"")

I've successfully created a dictionary with the color code and pieces as keys, but I want to use the values from the [pieces] dictionary as values for the tot_pieces dictionary.

Edit: the output that I'm expecting is another dictionary containing the product of pieces & color as keys and the values from the pieces dictionary as the values for this new dictionary.

Any help is appreciated!!!

CodePudding user response:

this can help you, (code in your fashion and will cover all cases)

dict(zip(
    [''.join(t) for t in list(itertools.product(theBoard['color'], theBoard['pieces']))],
    [t[1] for t in list(itertools.product(theBoard['color'], theBoard['pieces'].values()))],
))
  • Related