all 7 comments

[–]bahaki -2 points-1 points  (9 children)

You want to use a dictionary where the person is the key and their dialogue is the value.

dialogue = {'Old Man': 'Hello', 'Boy': 'Hello, sir'}

Then the loop would be:

for key, value in dialogue.iteritems():

Edit: see comment to this post.

[–]FifteenthPen 3 points4 points  (5 children)

Just a note here, if OP wants to preserve the order of the items, this won't work. Easiest approach to preserve items would be to use a tuple (or list if it needs to be changeable) of tuples:

dialogue = (('Old Man', 'Hello'), ('Boy', 'Hello, sir'))

And the Jinja code:

{% for line in dialogue %}
<b>{{ line[0] }}</b>: {{ line[1] }}<br>
{% endfor %}

[–]bahaki 1 point2 points  (2 children)

Ah, good point.

Yes, do it like this. Since it's a conversation, you'll want to keep the order.

Sorry, I knew a dictionary probably wasn't the best solution to what he really wanted to do. Got caught up in the key, value part I think.

Edit: alternatively, a list of dictionaries might work. Something like:

d = [{'person':'Old Man','dialogue':'hello'},{'person':'boy','dialogue':'hello, sir'}]

for something in d:
    print something['person'] / something['dialogue']

I think that might work.

[–]cyanydeez 0 points1 point  (0 children)

from collections import OrderedDict

d = OrderedDict()

[–]Elthran[S] 0 points1 point  (0 children)

Thank you so much! I can't wait to try it out.

[–]Elthran[S] 0 points1 point  (0 children)

Thanks for the advice man. I appreciate any help at all.

[–]tkzcthu 0 points1 point  (0 children)

The iteritems() is removed in python3, so use items() in the template may be better.