My situation is following. I have a sting "list" like this
Text1
Text2: value1
Text3:
Text4:
Text5: value2
...
Now I want to split the text into a dictionary with Key-Value Pair.
I tryed it with this 1liner
sp = dict(s.split(':') for s in list.split('\n') if len(s) > 1 and s.count(':') > 0)
This works great until there is no value like in Text3 and Text4.
My final dictionary should look like this
{ Text2:value1,Text3:'',Text4:'',Text5:value2 }
Text1 should be skipped - but Text3 & Text4 I need in the dictionary, also if the value is empty.
list
keyword for a variable. Instead of doings.count(':') > 0
, you can just do':' in s
– Teshan Shanuka J