Convert a list to a dictionary in Python

前端 未结 12 2131
無奈伤痛
無奈伤痛 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:17

    I am also very much interested to have a one-liner for this conversion, as far such a list is the default initializer for hashed in Perl.

    Exceptionally comprehensive answer is given in this thread -

    • python convert list to dictionary

    Mine one I am newbie in Python), using Python 2.7 Generator Expressions, would be:

    dict((a[i], a[i + 1]) for i in range(0, len(a) - 1, 2))

提交回复
热议问题