Reputation: 31568
I have two dictionaries which i want to compare the values. Like
Dict1['var1'] = 20
Dict1['var2'] = 30
Dict2['var1'] = 23
Dict2['var1'] = 26
Now i want to copare them and store the result like true or false in the same dict like this
if (Dict1['var1'] < Dict2['var1'])
Dict2['var1']['result'] = true
Becasue in my django template i want to show the color of table row as green if the result is true.
Whats the best way i can do that
Upvotes: 0
Views: 85
Reputation: 330
Because Dict2['var1']
is assigned as int type, the Dict2['var1']['result']
cannot be a dict type.
You can try this:
Dict1['var1'] = {'value':20, 'result':None}
Dict1['var2'] = {'value':30, 'result':None}
Dict2['var1'] = {'value':23, 'result':None}
Dict2['var2'] = {'value':26, 'result':None}
if Dict1['var1']['value'] < Dict2['var1']['value']:
Dict2['var1']['result'] = True
Upvotes: 2
Reputation: 53386
If you are want to check and decide in django template, you do not need to pre-compare and store the result. You can directly compare them in template itself.
{%if Dict1.var1 < Dict2.var1 %}
{# render to show green #}
{%else%}
{# do something else #}
{%endif}%
Upvotes: 0