使用 Python3,我想减少 if-elif 代码

Using Python3, i want to reduce if-elif code

嗨,在我的代码示例下方。有没有办法减少代码行数,节省时间?

示例.. 使用函数.. 我需要任何其他方法。请帮忙

enter image description here

是的。您可以从 globals() 按名称获取列表。请参阅以下示例:

list1 = []
list2 = []

msg = {
    'type': 2
}

def addToList(listNumber, msg):
    globals()["list" + str(listNumber)].append(msg)

for key, val in msg.items():
    if key == 'type':
        addToList(val, msg)

print(list1) #[]
print(list2) #[{'type': 2}]