How to split strings in list into a dictionary (key-value pairs) in Python

1 Answer

0 votes
string = 'aa-python bbb-java qq-c zzzz-c++'

lst = ['aa-python', 'bbb-java', 'qq-c', 'zzzz-c++']

dic = dict(keyvalue.split('-') for keyvalue in lst)

print(dic)



'''
run:

{'aa': 'python', 'bbb': 'java', 'qq': 'c', 'zzzz': 'c++'}

'''

 



answered Aug 7, 2022 by avibootz
...