0

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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}%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.