Python – How to split string into a dict

Few Python examples to show you how to split a string into a dictionary. 1.1 Split a string into a dict. #!/usr/bin/python str = "key1=value1;key2=value2;key3=value3" d = dict(x.split("=") for x in str.split(";")) for k, v in d.items(): print(k, v) Output key1 value1 key2 value2 key3 value3 1.2 Convert two list into a dict. #!/usr/bin/python str1 …

Read more

Python – How to split a String

Few examples to show you how to split a String into a List in Python. 1. Split by whitespace By default, split() takes whitespace as the delimiter. alphabet = "a b c d e f g" data = alphabet.split() #split string into a list for temp in data: print temp Output a b c d …

Read more