![Running a for-loop over a Jinja2 dictionary](/images/blogHeaderImages/2012-09-09-iterating_over_a_dict_in_a_jinja_template.png)
Running a for-loop over a Jinja2 dictionary
At the time of this posting, iterating over dictionaries inside a Jinja template is poorly documented, especially if you need access to the jinja dictionary keys and it's something I end up doing alot.
Of course running a for-loop over a jinja dictionary is as basic an activity as you're likely to do, for whatever reason it's buried in the Jinja2 documentation. I see thousands of people trying to find out how to do it hitting this page every day.
So here's how you do it, nice and simple...
#1: The Simple Way
Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
#2: Using iteritems()
If you need to retain both the key and value when doing a for-loop over a jinja dictionary, use iteritems() like this...
<ul>
{% for key, value in _dict.iteritems() %}
<dt>{{ key }}</dt>
<dd>{{ value }}</dd>
{% endfor %}
</dl>
Note, however, that Python dicts are not ordered; so you might want pass a sorted list of tuples instead.
Doing these things in Jinja is easy after you know. See the full API: http://jinja.pocoo.org/docs/api/
To: @mattccrampton
0
Other Posts
![Updating File Create Date From Exif Data](/images/blogHeaderImages/2022-03-28-updating-file-create-date-from-exif-data.png)
When exporting photos from a service like Flickr, perhaps after they've given notice that they're going to delete our photos if you don't subscribe to......
![All Truthy and Falsy Javascript Values](/images/blogHeaderImages/2020-04-28_all_truthy_and_falsy_javascript_values.png)
All Truthy and Falsy Javascript Values In Nodejs, every value has an associated boolean, true or false, value. For example, a null value has an......
![Google Forcing Nest Cameras Visual Indicator Light To Be On](/images/blogHeaderImages/2019-08-14-Google_forcing_nest_cameras_visual_indicator_light_to_be_on.png)
Google Forcing Nest Cameras Visual Indicator Light To Be On Received the following email from Google today... Full email text... Recently, we shared our commitment......
![Posting to Twitter with Python - Part Two - Posting Photos](/images/blogHeaderImages/2019-07-06-step_by_step_tutorial_to_post_to_twitter_using_python_part_two-posting_with_photos.png)
Posting to Twitter with Python - Part Two: Posting Photos NOTE: This is part two of my posting to Twitter with Python tutorial. If you......
![Doubleclick to open a file in VIM from OSX](/images/blogHeaderImages/2019-02-19-doubleclick_to_open_in_vim_on_osx.png)
Doubleclick to open a file in VIM from OSX I use VIM for just about everything from note taking to coding to keeping track of......
![Sign Into Gmail Without Signing Into Google Chrome](/images/blogHeaderImages/2018-09-26-sign-into-gmail-without-signing-into-google-chrome.png)
Sign Into Gmail Without Signing Into Google Chrome Unfortunately, Google has made changes to Chrome since this blog post was posted which removed the options......
Other Software Development Blog Posts
![Updating File Create Date From Exif Data](/images/blogHeaderImages/2022-03-28-updating-file-create-date-from-exif-data.png)
![All Truthy and Falsy Javascript Values](/images/blogHeaderImages/2020-04-28_all_truthy_and_falsy_javascript_values.png)
![Google Forcing Nest Cameras Visual Indicator Light To Be On](/images/blogHeaderImages/2019-08-14-Google_forcing_nest_cameras_visual_indicator_light_to_be_on.png)
![Posting to Twitter with Python - Part Two - Posting Photos](/images/blogHeaderImages/2019-07-06-step_by_step_tutorial_to_post_to_twitter_using_python_part_two-posting_with_photos.png)
![Doubleclick to open a file in VIM from OSX](/images/blogHeaderImages/2019-02-19-doubleclick_to_open_in_vim_on_osx.png)
![Sign Into Gmail Without Signing Into Google Chrome](/images/blogHeaderImages/2018-09-26-sign-into-gmail-without-signing-into-google-chrome.png)
![Four Steps To Write An OSX AppleScript to Gather ALL Windows](/images/blogHeaderImages/2018-08-03-applescript_to_gather_all_windows_on_osx.png)
![Four Simple Steps To Post To Twitter Using Python](/images/blogHeaderImages/2017-11-03-step_by_step_tutorial_to_post_to_twitter_using_python.jpg)
![The Mega List Of Free Image Sites For Blogging](/images/blogHeaderImages/2017-11-01-mega_list_of_free_image_sites_for_blogging.jpg)
![A VIM User's Experiments With GUI Code Editors](/images/blogHeaderImages/2017-10-30-a_vim_users_experiments_with_gui_code_editors.png)
![All Python Assert Methods In One List](/images/blogHeaderImages/2017-08-04-a_list_of_all_python_assert_methods.png)
![Invalidate All Cloudfront Files](/images/blogHeaderImages/invalidating_all_files_from_a_cloudfront_distribution_cache_from_the_command_line.png)
![Query an NTP server from Python](/images/blogHeaderImages/88291892461.png)
![Mac Terminal WiFi Commands](/images/blogHeaderImages/64144666914.png)
![Running a for-loop over a Jinja2 dictionary](/images/blogHeaderImages/2012-09-09-iterating_over_a_dict_in_a_jinja_template.png)
Other Blog Posts
![209 Netfix Categories You Dont Know About](/images/blogHeaderImages/2018-09-12-209-netflix-categories-you-dont-know-about.png)
![Easy Recipe For Making Chili at Home with Matt Crampton](/images/blogHeaderImages/2017-11-09-how_to_make_easy_chili.png)
Media Appearances
![Wall Street Journal Live Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-wsj_live_interview.png)
![NBC Press Here interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-nbc_press_here.png)
![Mobile Monday Silicon Valley panel with Matt Crampton](/images/blogHeaderImages/2017-10-26-mobile_monday_panel.png)
![Human Cloud panel with Matt Crampton](/images/blogHeaderImages/2017-10-26-human_cloud_panel.png)
![Fox Business Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-fox_business_interview.png)
![Fox Business interview on furloughed government workers with Matt Crampton](/images/blogHeaderImages/2017-10-26-fox_business_furloughed_workers_interview.png)
![Fortune Magazine Live Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-fortune_magazine_interview.png)
![CCTV Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-cctv_interview.png)
![8th and Walton show interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-8th_walton_interview.png)
Print Interviews
![MLive Interview with Matt Crampton](/images/blogHeaderImages/2019-04-25-mlive_interview.png)
![Techcrunch Interview on Gigwalk with Matt Crampton](/images/blogHeaderImages/2017-10-26-techcrunch_interview.png)
![Salon.com Labormetrics Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-salon_labormetrics_interview.png)
![Huffington Post Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-huffington_going_against_the_flow.png)
![CBS Pulse Interview with Matt Crampton](/images/blogHeaderImages/2017-10-26-cbs_pulse_article.png)
![Silicon Valley Business Journal - Best CIO - Matt Crampton](/images/blogHeaderImages/2014-06-20-silicon_valley_business_journal_best_cio_matt_crampton.png)