def commandComposer(input1, action, input2): composer1 = {"first":"A", "second":"B", "third":"C"} composer2 = {"first":"A", "second":"B"} commandDict = {"action1":f"TEST{composer2[input1]}{input2:02X}", "action2":f"TEST{composer1[input1]}{input2:04X}"} if action == "action1": print(commandDict["action1"].format()) elif action =="action2": print(commandDict["action2"].format()) commandComposer("first", "action2", 0x0) commandComposer("second", "action2", 0x0) # the line below return key error for third # commandComposer("third", "action2", 0x0) # if i modify "composer2 = {"first":"A", "second":"B", "third":0}" # in thiss way work correctly