I have a list tht contains strings, and those string could be a simple string or key value pair in a string. Please see below sample
c = ['Success, no result', 'num_parts: 55', 'num_boards: 2', 'xyz', 'adsqwrqw']
I know a way to Convert String to List of dictionaries using json.loads() replace()
method. But the condition is to have all elements as dictionary.
But in my case, a list contains a string, and dictionary as a string. How to filter in such condition?
Any suggestion
CodePudding user response:
I think you can check if ': ' is in the string by a simple if command:
for string in c:
if ': ' in string:
#do something with dictionary string
else:
#do something with simple string
CodePudding user response:
You can check for th ":"
in the string as that is the key which differentiates dictionary compliant strings like this :-
lst = ['Success, no result', 'num_parts: 55', 'num_boards: 2', 'xyz', 'adsqwrqw']
DictL, StrL = [], []
for i in lst:
if ":" in i:
DictL.append(i)
else:
StrL.append(i)