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

前端 未结 5 1842
佛祖请我去吃肉
佛祖请我去吃肉 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:31

    Simply use list comprehension.

    a = [51,27,13,56]  
    b = dict( [ (i,a[i]) for i in range(len(a)) ] )
    print b
    

提交回复
热议问题