Looking for a way to use an array to filter a list of dicts, selecting items that match a keys value

I’m certain there is a way to do this, but it’s just not coalescing in my head. I would like to avoid loops, because this is going to be used to generate a string (after a map and a join).

Example:

list:
 - one
 - two
 - three

dictionary_list:
   - item1: something
     item2: something else
     item3: something completely different
     item4: three
   - item1: larry
     item2: brother daryl
     item3: other brother daryl
     item4: two
   - item1: fee
     item2: fi
     item3: fo 
     item4: fum
   - item1: What's your name?
     item2: What's your quest?
     item3: What's your favorite color?
     item4: one

And I would like the output to be:


output_list:
   - item1: something
     item2: something else
     item3: something completely different
     item4: three
   - item1: larry
     item2: brother daryl
     item3: other brother daryl
     item4: two
   - item1: What's your name?
     item2: What's your quest?
     item3: What's your favorite color?
     item4: one

It would also be workable if I ended up just having a list of values from a particular key, but that key is not the key I want to filter by.

Any thoughts?

Sure, it’s possible. But I’m not going to claim it’s pretty.

{{ dictionary_list | map('dict2items') | map('selectattr', 'value', 'in', list) | zip(dictionary_list) | selectattr(0) | map(attribute=1) }}

Why not simply {{ dictionary_list | selectattr('item4', 'in', list) }}?

(Assuming you know the value’s always in item4. Which is the case in your example. If it is not, @flowerysong’s solution is better.)

Not pretty at all, but it gets the job done. I have pity for any of our junior engineers that run into this code. lol.

Thank you.