Beginning with a string like:
S='a=65 b=66 c=67'
How would you create an output a dict like {'a':'65','b':'66','c':'67'}
Attempt:
S='a=65 b=66 c=67'
L=s.split(' ')
D=dict()
A=''
i=0
While i<Len(L):
A=L[i].split('=')
D[a[i]]=a[i+1]
i+2
print (D)
Error on line 8 indexerror list index out of range
Let's use comprehension and split:
dict(i.split('=') for i in S.split())
Output:
{'a': '65', 'b': '66', 'c': '67'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With