# Creating new list from values in list of dicts

**URL:** <https://forum.ansible.com/t/creating-new-list-from-values-in-list-of-dicts/19251>\
**Category:** Ansible Project\
**Created:** [September 14, 2015, 1:19am UTC](https://forum.ansible.com/t/creating-new-list-from-values-in-list-of-dicts/19251 "2015-09-14T01:19:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [September 14, 2015, 1:19am UTC](https://forum.ansible.com/t/creating-new-list-from-values-in-list-of-dicts/19251/1 "2015-09-14T01:19:47Z")

</div>

Hi all,

I am using the ec2\_vpc\_subnet\_facts module to get back a list of subnet dicts. I want to get the id from each of these dicts and add it to a new list so i end up with just a list of subnet ids. I thought this might be possible with existing jinja filters but i’m not sure where to start so any pointers would be appreciated.

Example:

‘subnets’: [{u’availability\_zone’: u’ap-southeast-2a’, u’tags’: {u’Type’: u’public’, u’Zone’: u’a’, u’Env’: u’support’, u’Name’: u’support-public-a’}, u’default\_for\_az’: u’false’, u’state’: u’available’, u’vpc\_id’: u’vpc-11223344’, u’cidr\_block’: u’10.66.128.0/25’, u’available\_ip\_address\_count’: 113, u’id’: u’subnet-11223344’, u’map\_public\_ip\_on\_launch’: u’false’}, {u’availability\_zone’: u’ap-southeast-2b’, u’tags’: {u’Type’: u’public’, u’Zone’: u’b’, u’Env’: u’support’, u’Name’: u’support-public-b’}, u’default\_for\_az’: u’false’, u’state’: u’available’, u’vpc\_id’: u’vpc-11223344’, u’cidr\_block’: u’10.66.129.0/25’, u’available\_ip\_address\_count’: 118, u’id’: u’subnet-55667788’, u’map\_public\_ip\_on\_launch’: u’false’}]

I want to get each subnet.id in to a list so i end up with…

my\_list: [subnet-11223344, subnet-55667788]

Thanks,

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [September 14, 2015, 1:55am UTC](https://forum.ansible.com/t/creating-new-list-from-values-in-list-of-dicts/19251/2 "2015-09-14T01:55:34Z")

</div>

So I wrote my own filter for this…

`  
def idfromlistofdicts(l):  
result =   
for item in l:  
result.append(item[‘id’])

return result

class FilterModule(object):  
def filters(self):  
return {  
‘idfromlistofdicts’: idfromlistofdicts,  
}  
`

Not sure if something like this already exists though.

---

<div class="post-metadata">

**Author:** ![Takeshi\_Yaegashi](https://avatars.discourse-cdn.com/v4/letter/t/838e76/32.png) [@Takeshi\_Yaegashi](https://forum.ansible.com/u/Takeshi_Yaegashi)\
**Post date:** [September 14, 2015, 4:31am UTC](https://forum.ansible.com/t/creating-new-list-from-values-in-list-of-dicts/19251/3 "2015-09-14T04:31:14Z")

</div>

Hello,

Try the following playbook:

`
