A list as a key for a dictionary

前端 未结 5 1954
一整个雨季
一整个雨季 2021-01-22 08:28

I have multiple lists of tuples eg

[([1, 2, 3, 4], 2), ([5, 6, 7], 3)]

that I would like to have as keys to a dictionary (so each key in my dic

5条回答
  •  长发绾君心
    2021-01-22 08:46

    Convert your lists to tuples:

    dict((tuple(a), b) for a,b in [([1,2,3,4],2),([5,6,7],3)])
    

    If you are using Python >= 2.7 you can use dict-comprehensions:

    {tuple(a): b for a,b in [([1,2,3,4],2),([5,6,7],3)]}
    

提交回复
热议问题