Convert two lists into a dictionary

前端 未结 18 2557
囚心锁ツ
囚心锁ツ 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:06

    you can use this below code:

    dict(zip(['name', 'age', 'food'], ['Monty', 42, 'spam']))
    

    But make sure that length of the lists will be same.if length is not same.then zip function turncate the longer one.

提交回复
热议问题