Home > Mobile >  how to compare two dictionaries and display the values that differs in template?
how to compare two dictionaries and display the values that differs in template?

Time:12-01

I try to compare two dictionaries and if on key, value differs from the other dictionary then print the difference key, value in red.

I think my views.py is correct. But how to show the difference in the template?

So I have views.py:


def data_compare():
    fruits = {
        "appel": 3962.00,
        "waspeen": 3304.07,
        "ananas": 24,
    }
    set1 = set([(k, v) for k, v in fruits.items()])
    return set1


def data_compare2():

    fruits2 = {
        "appel": 3962.00,
        "waspeen": 3304.07,
        "ananas": 30,
    }

    set2 = set([(k, v) for k, v in fruits2.items()])
    return set2


def data_combined(request):
    data1 = data_compare()
    data2 = data_compare2()

    diff_set = list(data1 - data2)   list(data2 - data1)
    
    print(data1)

    return render(request, "main/data_compare.html", context={"data1": data1, "data2": data2, "diff_set": diff_set})

and template:

  <!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>

    <body>

        <div >
            {% for key, value in data1 %}
            <span   {% if diff_set %}   style="color: red;">{% endif %} {{ key }}: {{value}}</span><br>
            {% endfor %}
        </div>

        <div >
            {% for key, value in data2 %}
            <span {% if diff_set %}  style="color: red;">{% endif %}{{ key }}: {{value}}</span><br>
            {% endfor %}
        </div>

    </body>

</html>

I did a print(diff_set) and that shows:

[('ananas', 24), ('ananas', 30)]

so that is correct

But everything is now red. and only in this case ananas has to be red

Question: how to return the key, value from a dictionary that differes from the other dictionary in red?

CodePudding user response:

Looks dictdiff might be useful in your case. The following example is not the same as your output, but I hope it is useful.

import dictdiffer

fruits = {
    "appel": 3962.00,
    "waspeen": 3304.07,
    "ananas": 24,
}
fruits2 = {
    "appel": 3962.00,
    "waspeen": 3304.07,
    "ananas": 30,
}

diff = list(dictdiffer.diff(first=fruits, second=fruits2))
print(diff)  # [('change', 'ananas', (24, 30))]

CodePudding user response:

Following the same principle from my last answer. Since you are trying to compare two dictionaries with same keys, you can just iterate over both of them at once, compare the values and if they differ append the key to a condition list:

def compare_data(request):
    fruits = {"appel": 3962.00,"waspeen": 3304.07,"ananas": 24,}
    fruits2 = {"appel": 3962.00,"waspeen": 3304.07,"ananas": 30,}
    diff_set = []

    for k, v in fruits.items():
        if fruits[k] != fruits2[k]:
            diff_set.append(k)

    context = {
        'fruits': fruits,        
        'fruits2': fruits2,        
        'diff_set': diff_set,        
    }
    return render(request, 'main/data_compare.html', context)

template.html:

{% extends 'base.html' %}

{% block content %}
    <div >
        {% for key, value in fruits.items %}
            <span {% if key in diff_set %} style="color: red;" {% endif %}>{{ key }}: {{value}}</span><br>
        {% endfor %}
    </div>

    <div >
        {% for key, value in fruits2.items %}
            <span {% if key in diff_set %} style="color: red;"{% endif %}>{{ key }}: {{value}}</span><br>
        {% endfor %}
    </div>
{% endblock %}

Edit

In your case, everything is showing red because of your IF statement:

{% if diff_set %}...{% endif %}

Which checks if the 'diff_set' variable contains any values. It does, so it returns True every iteration.

In order to use your 'diff_set' data structure: [('ananas', 24), ('ananas', 30)]

One needs to loop through the list and check if if the first value of the tuple is equal to the key value. Even if you do that, with current html structure it would print 'ananas' in red twice.

  • Related