Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to join a list of dictionaries by dict val. Jinja2

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading