Iterating Through Two Lists in Django Templates Django.How

Author avatar wrote on 06/06/2022

Views.py

zipped_segments = zip(source_segments, target_segments)
for s_segment, t_segment in zipped_segments:
    print('s_segment', s_segment)
    print('t_segment', t_segment)
    print('---------------------')


Template

{% for s_segment,t_segment in zipped_segments %}
    {{s_segment}}{{t_segment}}
{% endfor %}

Create a dictionary using two lists


zipped_segments = zip(source_segments, target_segments)

Convert two lists into a dictionary


keys = ['name', 'age', 'food']
values = ['Monty', 42, 'spam']

To

a_dict = {'name': 'Monty', 'age': 42, 'food': 'spam'}

Solution

keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}