Search a dictionary key and fetch it's values based on another list

39 Views Asked by At

I have a list like account_id_list = [1,2,3,4,5,6,7]

I have a dictionary like tag_dict = [1:{"a"= "b", "y"= "z"}, 2:{"a"= "b", "y"= "z"}, 3:{"a"= "b", "y"= "z"}]

I want to perform an operation in jinja2 such that i should be able to do a for loop on account_id_list and using the value 3 as a reference i should be able to fetch the key i.e 3 in tag_dict and fetch its values.

I am pretty new to jinga and i tried to use a for loop inside a for loop but it's running for a long time and i want to know if there is a easy way of doing it.

1

There are 1 best solutions below

1
On BEST ANSWER

Your tag_dict looks malformed. Did you mean this?

tag_dict = {1:{"a":"b","y":"z"},2:{"a":"b","y":"z"},3:{"a":"b","y":"z"}}

This should work for you:

{%- set account_id_list = [1,2,3,4,5,6,7] -%}

{%- set tag_dict = {1:{"a":"b","y":"z"},2:{"a":"b","y":"z"},3:{"a":"b","y":"z"}} -%}

{% for value in account_id_list %}
  {{ tag_dict[value] }}
{% endfor %}