Q:

Group by dictionary value in for loop

I'm trying to group by dictionary value inside for loop in an django template.
My template is as follows : 
<div id="engines-rows">
    {% for engine in engines %}
        <h3>{{ engine }}</h3>
    {% endfor %}
</div>

Result that I get is as follows :
{'id': 8, 'name': '2.0 TFSI', 'fuel_type': 'PETROL', 'displacement_cc': 1984, 'power_hp': 188, 'power_ps': 190, 'power_kw': 140}
{'id': 7, 'name': '2.0 TDI', 'fuel_type': 'DIESEL', 'displacement_cc': 1968, 'power_hp': 181, 'power_ps': 184, 'power_kw': 135}
{'id': 6, 'name': '2.0 TDI', 'fuel_type': 'DIESEL', 'displacement_cc': 1968, 'power_hp': 148, 'power_ps': 150, 'power_kw': 110}
{'id': 5, 'name': '1.6 TDI', 'fuel_type': 'DIESEL', 'displacement_cc': 1598, 'power_hp': 109, 'power_ps': 110, 'power_kw': 81}
{'id': 4, 'name': '1.4 TFSI g-tron', 'fuel_type': 'CNG', 'displacement_cc': 1395, 'power_hp': 109, 'power_ps': 110, 'power_kw': 81}
{'id': 3, 'name': '1.4 TFSI e-tron', 'fuel_type': 'ELECTRICITY', 'displacement_cc': 1395, 'power_hp': 148, 'power_ps': 150, 'power_kw': 110}
{'id': 2, 'name': '1.4 TFSI', 'fuel_type': 'PETROL', 'displacement_cc': 1395, 'power_hp': 148, 'power_ps': 150, 'power_kw': 110}
{'id': 1, 'name': '1.0 TFSI', 'fuel_type': 'PETROL', 'displacement_cc': 999, 'power_hp': 114, 'power_ps': 116, 'power_kw': 85}

I want to group by fuel_type, thus I want to see something like :
Petrol
Name: 2.0 TFSI  --  displacement_cc: 1984  --  power_hp: 188  --  power_ps: 190  --  power_kw: 140
Name: 1.4 TFSI  --  displacement_cc: 1395  --  power_hp: 148  --  power_ps: 150  --  power_kw: 110
........
Diesel
Name: 2.0 TDI  --  displacement_cc: 1968  --  power_hp: 181  --  power_ps: 184  --  power_kw: 135
Name: 2.0 TDI  --  displacement_cc: 1968  --  power_hp: 148  --  power_ps: 150  --  power_kw: 110
....
............
I cant do it something like
<div id="engines-rows">
    {% for engine in engines %}
        {% if engine.fuel_type == "PETROL" %}
            # Do something
        {% endif %}
        {% if engine.fuel_type == "DIESEL" %}
            # Do something
        {% endif %}
         # And so on
    {% endfor %}
</div>

But can I do it dynamically, I do not want to add if for every fuel type.
Thanks

A:

I solved it as follows : 
<div id="engines-rows">
    {% regroup engines|dictsort:"fuel_type" by fuel_type as fuel_type_list %}
    {% for fuel_type in fuel_type_list %}
        <div style="margin-bottom: 15px;">
            <div style="margin-bottom: 5px;background-color: #263238; color: #ffffff;height: 40px;display: block;padding: 10px 15px;border: 1px solid #ddd;">{{ fuel_type.grouper }}</div>
            {% for engine in fuel_type.list %}
                <a id="engine-row" class="col-lg-12" style="margin-bottom: 5px;" data-engine-id={{ engine.id }}>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.name }}</div>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.displacement_cc }} ccm</div>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.power_ps }} pk</div>
                    <div class="col-lg-3" data-engine-id={{ engine.id }}>{{ engine.power_kw }} kW</div>
                </a>
                <div class="clearfix" />
            {% endfor %}
        </div>
    {% endfor %}
</div>

The YK Li's solution gave my :
Petrol
Name: 2.0 TFSI -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
Diesel
Name: 2.0 TDI -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
Name: 2.0 TDI -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
Name: 1.6 TDI -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
CNG
Name: 1.4 TFSI g-tron -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
ELECTRICITY
Name: 1.4 TFSI e-tron -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
Petrol
Name: 1.4 TFSI -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140 
Name: 1.0 TFSI -- displacement_cc: 1984 -- power_hp: 188 -- power_ps: 190 -- power_kw: 140
That was not what I wanted.
In the docs (the link that Ilya V. Schurov posted) stays that the solution is to sort the data in the template using the dictsort filter, if your data is in a list of dictionaries:

