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 find max value from a list of numbers stored as string in a Jinja template?

I have a list of integers stored in a list. Apparently it is stored as string in the datastore. So my current list of integers are as:

price = ['31412', '12300', '23000']

I want to get the highest number from the list.

{% for i in price %}
highest price is: {{ i | max }}
{% endfor %}

Expected output:

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

highest price is: 31412

What I get is:

highest price is: 4

How can I do this correctly?

>Solution :

You need to first convert each string in the list to an integer before finding the maximum.

According to my understanding of the documentation you should be able to use the map filter like so:

highest price is: {{ price | map('int') | max }}

This should be equivalent to the Python code max(map(int, price)).

You don’t need a for construct. The max filter already implicitly loops over the price list.

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