Convert two lists into a dictionary

前端 未结 18 2498
囚心锁ツ
囚心锁ツ 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 04:57

    For those who need simple code and aren’t familiar with zip:

    List1 = ['This', 'is', 'a', 'list']
    List2 = ['Put', 'this', 'into', 'dictionary']
    

    This can be done by one line of code:

    d = {List1[n]: List2[n] for n in range(len(List1))}
    

提交回复
热议问题