Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How do I Iterate through a list of dictionaries to concatenate values from a same element

Tags:

python

django

I have a list of dictionaries as follows:

listDict = [{'product':'sandwich','price':'5200'}, {'product':'hamburger','price':'3000'}]

to iterate through the elements i do:

{%for element in listDict%}
    {% for key,value in element.items %}
        <input type="checkbox" name = "bar" value = "{{ value }}">{{ value }}<br>
    {% endfor %}
{% endfor %}                    

this, as expected, will print:

sandwich

5200

hamburger

3000

but how could I concatenate the values in order to print something like this:

sandwich - 5200

hamburger - 3000

I can't do something like below:

for element in listDict:
    element['product']+" - "+element['price']

Thanks in advance!

like image 401
Lucas Avatar asked Jan 21 '12 01:01

Lucas


1 Answers

You can just do this:

{%for element in listDict%}
    {{ element.product }} - {{ element.price }}
{% endfor %}
like image 199
Rob Wouters Avatar answered Nov 13 '22 18:11

Rob Wouters