Converting a single ordered list in python to a dictionary, pythonically

前端 未结 6 786
悲&欢浪女
悲&欢浪女 2021-01-07 14:00

I can\'t seem to find an elegant way to start from t and result in s.

>>>t = [\'a\',2,\'b\',3,\'c\',4]
#magic
>>>print s
         


        
6条回答
  •  醉话见心
    2021-01-07 14:49

    Not exactly efficient, but if you don't need it for very large lists:

    dict(zip(t[::2], t[1::2]))
    

    Or your version using a generator:

    dict(t[i:i+2] for i in xrange(0, len(t), 2))
    

提交回复
热议问题