Tuesday, August 30, 2016

Re: Django template iterating through two dictionaries/variables

This is why i ask this group.

Everyone helped out, but  Nate's link pushed me in the right direction.

Sometimes I don't even know what to ask stack overflow, but the google group always helps out.


On Tuesday, August 30, 2016 at 11:28:37 AM UTC-5, Nate Granatir wrote:
You could also just join the dicts before passing them to the template. It looks like it's relatively straightforward (scores is just a list that contains dicts of a 'Number' and a 'score', right?) Then you could turn scores into a dict, assuming Numbers are unique:

scores_new = {score['Number']:score['score'] for score in scores}

And then update the persons dict to include the score:
for person in persons:
    person.update({'score': scores_new.get(person['Number'])})

Then in your persons list in the template you'll just need to display person.score.

If you want to get really fancy (if, say, there's more than just a score you need to pull from the dicts in scores) you can see how to merge two lists of dicts on a common key here:

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/49018a13-680a-4b6d-8d1e-be5c5444c2d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment