# use of .keys() and .values() on dict object

**URL:** <https://forum.ansible.com/t/use-of-keys-and-values-on-dict-object/34271>\
**Category:** Ansible Project\
**Created:** [December 2, 2020, 1:41pm UTC](https://forum.ansible.com/t/use-of-keys-and-values-on-dict-object/34271 "2020-12-02T13:41:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Caillibaud](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Daniel\_Caillibaud](https://forum.ansible.com/u/Daniel_Caillibaud)\
**Post date:** [December 2, 2020, 1:41pm UTC](https://forum.ansible.com/t/use-of-keys-and-values-on-dict-object/34271/1 "2020-12-02T13:41:48Z")

</div>

Hi,

I saw the dict2items filter, which can be use with

# with this var  
ips:  
&nbsp;&nbsp;foo: 192.168.1.4  
&nbsp;&nbsp;bar: 1.2.3.4

# a task with a loop on it  
&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: "here is an ip {{ item.value }}  
&nbsp;&nbsp;loop: "{{ ips | dict2items | list }}"

When only keys or values are needed, the use of .keys() or .values() seems  
easier (especially with filters)

&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: "here is a private ip {{ item }}"  
&nbsp;&nbsp;loop: "{{ ips.values() | list | ipaddr('private') | default('') }}"

but I didn't find any mention of this in the official doc, neither in the  
examples I saw, is it better to avoid this way of looping ?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 3, 2020, 7:55am UTC](https://forum.ansible.com/t/use-of-keys-and-values-on-dict-object/34271/2 "2020-12-03T07:55:45Z")

</div>

In my opinion it is fine to use the methods of the underlying Python objects.

In some cases it is hard to solve without that, especially with  
nested variables:

Environment="FCGI\_CHILDREN={{ sympa.web.get(unit\_name + '\_procs') }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
