Creating a Dictionary from a List of 2-Tuples

前端 未结 2 1433
挽巷
挽巷 2021-01-05 18:06

I have a list of 2-tuples like this:

l = [(\'a\', 1), (\'b\', 2)]

and I want to be able to map this onto a dictionary object, so that I can

相关标签:
2条回答
  • 2021-01-05 18:20

    Why not just do this:

    d = dict(l)
    

    Also, to answer your question, your solution is failing because y (which is a 2-tuple) has no method update, since it's not a dict. Thankfully, what you're doing is built right in.

    0 讨论(0)
  • 2021-01-05 18:29
    >>> l = [('a', 1), ('b', 2)]
    >>> d = dict(l)
    >>> d['a']
    1 
    
    0 讨论(0)
提交回复
热议问题