One liner: creating a dictionary from list with indices as keys

前端 未结 5 1820
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 15:03

I want to create a dictionary out of a given list, in just one line. The keys of the dictionary will be indices, and values will be the elements of the list. Someth

5条回答
  •  一生所求
    2020-11-27 15:38

    With another constructor, you have

    a = [51,27,13,56]         #given list
    d={i:x for i,x in enumerate(a)}
    print(d)
    

提交回复
热议问题