Convert two lists into a dictionary

前端 未结 18 2602
囚心锁ツ
囚心锁ツ 2020-11-21 04:35

Imagine that you have:

keys = [\'name\', \'age\', \'food\']
values = [\'Monty\', 42, \'spam\']

What is the simplest way to produce the foll

18条回答
  •  面向向阳花
    2020-11-21 05:05

    Here is also an example of adding a list value in you dictionary

    list1 = ["Name", "Surname", "Age"]
    list2 = [["Cyd", "JEDD", "JESS"], ["DEY", "AUDIJE", "PONGARON"], [21, 32, 47]]
    dic = dict(zip(list1, list2))
    print(dic)
    

    always make sure the your "Key"(list1) is always in the first parameter.

    {'Name': ['Cyd', 'JEDD', 'JESS'], 'Surname': ['DEY', 'AUDIJE', 'PONGARON'], 'Age': [21, 32, 47]}
    

提交回复
热议问题