I'm working on a backend flask/jinja2 app and I know how to join a list of strings; however, I need to join a list of dictionaries. I want to join all the dictionaries values, which are strings, into one string... I'm not quite sure of the syntax in Jinja2 for this. an example of the dict is below.
[{key:'value'},{key:'secondValue'}]
with an expected output of
"value, secondValue"
CodePudding user response:
IIUC, you can use:
dict_list = [{'a':'aa', 'b':'bb'}, {'c':'cc', 'd':'dd'}]
', '.join([element for sublist in [x.values() for x in dict_list] for element in sublist])
Result:
'aa, bb, cc, dd'