Convert two lists into a dictionary

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

    You can also use dictionary comprehensions in Python ≥ 2.7:

    >>> keys = ('name', 'age', 'food')
    >>> values = ('Monty', 42, 'spam')
    >>> {k: v for k, v in zip(keys, values)}
    {'food': 'spam', 'age': 42, 'name': 'Monty'}
    

提交回复
热议问题