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

**URL:** <https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260>\
**Category:** Get Help\
**Created:** [July 12, 2024, 10:13pm UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260 "2024-07-12T22:13:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Incrediblestorm](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/incrediblestorm/32/3561_2.png) [@Incrediblestorm](https://forum.ansible.com/u/Incrediblestorm)\
**Post date:** [July 12, 2024, 10:13pm UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260/1 "2024-07-12T22:13:33Z")

</div>

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:

```yaml
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:

```yaml

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?

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [July 12, 2024, 11:21pm UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260/2 "2024-07-12T23:21:56Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 13, 2024, 10:59am UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260/3 "2024-07-13T10:59:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 13, 2024, 11:01am UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260/4 "2024-07-13T11:01:46Z")

</div>

(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.)

---

<div class="post-metadata">

**Author:** ![Incrediblestorm](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/incrediblestorm/32/3561_2.png) [@Incrediblestorm](https://forum.ansible.com/u/Incrediblestorm)\
**Post date:** [July 13, 2024, 12:26pm UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260/5 "2024-07-13T12:26:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 12, 2024, 12:26pm UTC](https://forum.ansible.com/t/looking-for-a-way-to-use-an-array-to-filter-a-list-of-dicts-selecting-items-that-match-a-keys-value/7260/6 "2024-08-12T12:26:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
