# Add {% withdict %} and {% withlist %} tags to allow creating dicts and lists in templates

**URL:** <https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658>\
**Category:** Django Internals\
**Created:** [October 14, 2024, 8:57am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658 "2024-10-14T08:57:00Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [October 14, 2024, 8:57am UTC](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658/1 "2024-10-14T08:57:00Z")

</div>

So this one may have been discussed before and I’m sure this feature was rejected before it may look like a antipattern and data should be computed in the view.

However, I feel like there are very valid use cases for creating lists and dicts locally in the template.

Ok, first, there is already the `{% with %} tag allowing to declare variables in context. And there may be use cases where the template needs to transform on or several variables in a list or a dict but pre-computing them in context is just code noise.

This is what I propose:

```django
{% withdict key1=some_variable key2=some_other_variable as my_dict %}
   {{ my_dict.key1 }}
{% endwithdict %}

```

```django
{% withlist variable1 variable2 variable3 as my_list %}
  {% for var in my_list %}
    {% include "some/template.html" with tpl_var=var %}
  {% enfor %}
{% endwithlist %}

```

I understand that `{% cycle %}` may fill the same use that `{% withlist %}` however — and this is personal test here — I don’t fully understand how it work. Plus it may be disturbing to use `{% with %}` to declare a simple var, `{% withdict %}` to declare a dict variable but `{% cycle %}` to declare a list so I think `{% withlist %}` has a place here, may it be just an wrapper around `{% cycle %}`.

---

_[View the full topic](https://forum.djangoproject.com/t/add-withdict-and-withlist-tags-to-allow-creating-dicts-and-lists-in-templates/35658)._
