Convert a list to a dictionary in Python

前端 未结 12 1997
無奈伤痛
無奈伤痛 2020-11-22 04:14

Let\'s say I have a list a in Python whose entries conveniently map to a dictionary. Each even element represents the key to the dictionary, and the following o

12条回答
  •  孤独总比滥情好
    2020-11-22 05:03

    You can also do it like this (string to list conversion here, then conversion to a dictionary)

        string_list = """
        Hello World
        Goodbye Night
        Great Day
        Final Sunset
        """.split()
    
        string_list = dict(zip(string_list[::2],string_list[1::2]))
    
        print string_list
    

提交回复
热议问题