Convert a list to a dictionary in Python

前端 未结 12 2197
無奈伤痛
無奈伤痛 2020-11-22 04:14

Let\'s say I have a list a in Python whose entries conveniently map to a dictionary. Each even element represents the key to the dictionary, and the following o

12条回答
  •  醉酒成梦
    2020-11-22 05:07

    Something i find pretty cool, which is that if your list is only 2 items long:

    ls = ['a', 'b']
    dict([ls])
    >>> {'a':'b'}
    

    Remember, dict accepts any iterable containing an iterable where each item in the iterable must itself be an iterable with exactly two objects.

提交回复
热议问题