>>> SOLVED <<< Nested Dictionaries Help

Could someone take a look at the following nested dictionaries? it is throwing a SyntaxError.

User_Decode_Table = {
                      {
                        "Rigid":
                             {
                             'Right Half Swing' : {
                                               "DIR__Value": 1,
                                               "Toy_Selc"  : 0
                                                   }
                             },
                             {
                              'Right Full Swing': {
                                                "DIR_Value":1,
                                                "Toy_Selc":1
                                                  }  
                            },
                            {
                                   'Left Half Swing':{
                                                "DIR_Value":1,
                                                "Toy_Selc":(0)
                                                     }
                            },
                            {
                                    'Left Full Swing':{
                                                "DIR_Value":1,
                                                "Toy_Selc":(1)
                                                      }
                            },
                     }
                     {
                            
                    "Flexible":  {
                                            'Right' {
                                                "DIR_Value": 1,
                                                "Toy_Selc": 2
                                                    }
                                 },
                                 {
                                            'Left':   {
                                                 "DIR_Value": 0,
                                                 "Toy_Selc": 2
                                                      }
                                },
                                {
                                            'Top' :  {      
                                                 "DIR_Value": 0,
                                                 "Toy_Selc": 3
                                                }
                                }
                      
                    }
        }



To summarise, what you’ve wrtten is like this:

User_Decode_Table = { # This a set
    { # This is a dict in the set
        "Rigid": # The key
            { # The value
                ...
            },
            { # A set?
                ...
            },
            { # Another set?
                ...
            },
            {
                ...
            },
    } # Missing comma after }
    { # This is a another dict in the set
        "Flexible": # The key
            { # The value
                ...
            },
            { # A set?
                ...
            },
            { # Another set?
                ...
            }
        }
}

What does it mean?
You write some dictionaries one after another.
Why?

I am using this nested dictionary to decode the user-selected options without using if statements. The following function shows how this works.

def ChangeToyType(ToyType, Postion):
	global Toy_Sel

#Debug Point
	print()
	print("====================================")
	print(" ChangeToyType >>  ToyType = ", ToyType, " || Postion = ", Postion )

	GPIO.output(DIR_Pin, User_Decode_Table[ToyType][Postion]['DIR_Value']) # Decoe the user selection to what direction to rotate
	
	Toy_Sel = User_Decode_Table[ToyType][Postion]['TOY_Type'] # Decoe the user toy selected
	GPIO.output(Toy_Sel_Pins, Toy_Pin_Table[Toy_Sel]) # Output what task the cnroler is to run

#Debug Point
	print("Exiting ChangeToyType >> Toy_Sel = ", Toy_Sel  )

#####################################################################

Toy_Pin_Table = {0:(0,0),
                 1:(0,1),
                 2:(1,0),
                 3:(1,1)
                }

If I understand correctly, this is what you actually want:

User_Decode_Table = {
    "Rigid": {
        'Right Half Swing': {
            "DIR__Value": 1,
            "Toy_Selc": 0
        },
        'Right Full Swing': {
            "DIR_Value": 1,
            "Toy_Selc": 1
        },
        'Left Half Swing': {
            "DIR_Value": 1,
            "Toy_Selc": (0)
        },
        'Left Full Swing': {
            "DIR_Value": 1,
            "Toy_Selc": (1)
        }
    },
    "Flexible":
        {
            'Right': {
                "DIR_Value": 1,
                "Toy_Selc": 2
            },
            'Left': {
                "DIR_Value": 0,
                "Toy_Selc": 2
            },
            'Top': {
                "DIR_Value": 0,
                "Toy_Selc": 3
            }
        }
}

You were getting a SyntaxError because of both too many {}s and missing commas.

Thank you Dan. :smiley: :smiley: :smiley:
I know it had to be something simple.

Why are the Left Half Swing and Left Full Swing Toy Selc numbers in ()?

I am marking this topic solved.

1 Like

That’s how you put them in your original code block - I just copy/pasted it and cleaned up the definitions.