Stack Exchange
How to remove duplicate fields and create a sub-list out of them in python?
[{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('d0576830-df96-4411-ac33-4bac597f2010')}, 

{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa')}, 

{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')}, 

{u'cloud_account': UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'), u'_id': UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')}])

The '_id' field refers to the ids of machine_groups, I would like to have a sub-list that regroups those ids for the first cloud account.

Can you post your expected output?

[{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('d0576830-df96-4411-ac33-4bac597f2010'),  u'_id': UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'), u'_id': UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')},
{u'cloud_account': UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'), u'_id': UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')}])


List of machine groups per cloud account
Something like this could help:
cloud_accounts = [
    {'cloud_account': 'a639efb6-d4e0-4929-b309-8171bf61b508', '_id': 'd0576830-df96-4411-ac33-4bac597f2010'},
    {'cloud_account': 'a639efb6-d4e0-4929-b309-8171bf61b508', '_id': 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'},
    {'cloud_account': 'a639efb6-d4e0-4929-b309-8171bf61b508', '_id': 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'},
    {'cloud_account': 'fd4bbf86-9803-4724-9576-d83355bdcff2', '_id': '31140d2e-15a1-4203-bdbb-55c14cd6b83a'}]

machine_groups_per_cloud_account = {d['cloud_account']: [] for d in cloud_accounts}
for d in cloud_accounts:
    machine_groups_per_cloud_account[d['cloud_account']].append(d['_id'])

import pprint
pprint.pprint(machine_groups_per_cloud_account)

Here is the output:
{'a639efb6-d4e0-4929-b309-8171bf61b508': ['d0576830-df96-4411-ac33-4bac597f2010', 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa', 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'], 
 'fd4bbf86-9803-4724-9576-d83355bdcff2': ['31140d2e-15a1-4203-bdbb-55c14cd6b83a']}

In the result example you gave, you have multiple times the same keys in a dict, it is incorrect, so the idea here is to consider cloud_account as python dict keys and append _id in a associated list (python dict value).
Number of machines per cloud account
machine_groups = [
    {'count': 1, '_id': '31140d2e-15a1-4203-bdbb-55c14cd6b83a'},
    {'count': 17, '_id': 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'},
    {'count': 1, '_id': 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'},
    {'count': 1, '_id': 'd0576830-df96-4411-ac33-4bac597f2010'}]
machine_groups_reorganized = {}
for m in machine_groups:
    machine_groups_reorganized[m['_id']] = m['count']

machine_count_per_cloud_account = {d['cloud_account']: 0 for d in cloud_accounts}
for d in cloud_accounts:
    machine_count_per_cloud_account[d['cloud_account']] += machine_groups_reorganized[d['_id']]

import pprint
pprint.pprint(machine_count_per_cloud_account)

Here is the output:
{'a639efb6-d4e0-4929-b309-8171bf61b508': 19,
 'fd4bbf86-9803-4724-9576-d83355bdcff2': 1}


Odly, it only returns an array of the cloud_accounts

I've got it working well using your data variable and my sample code as it is: res = {'a639efb6-d4e0-4929-b309-8171bf61b508': ['d0576830-df96-4411-ac33-4bac597f2010', 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa', 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'], 'fd4bbf86-9803-4724-9576-d83355bdcff2': ['31140d2e-15a1-4203-bdbb-55c14cd6b83a']}

When you say data you mean the first array I posted ?

@BrokenStrings yes indeed

Can you please share the whole code and how you tested it ?

@BrokenStrings I edited my answer with the full sample code

Thanks, it worked finally. My aim now is to create a dict fetching this one with [{u'count': 1, u'_id': UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')}, {u'count': 17, u'_id': UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')}, {u'count': 1, u'_id': UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa')}, {u'count': 1, u'_id': UUID('d0576830-df96-4411-ac33-4bac597f2010')}] in order to get an array containing number of machines per cloud_account

the previous array is the number of machines per machine_group

then you can increase a counter instead of creating an array of machines ID per machine group:
res = {d['cloud_account']: 0 for d in data}
for d in data: res[d['cloud_account']] += 1

actually, there is no way to get the number of machines per cloud_account other then getting first the number of machines per machine_group an the mahcine_groups per cloud_account, that's because I'm using mongodb

so what you suggested gives the number of machine_groups not the number of machines per cloud_account

@BrokenStrings I think you definitely need to change the way you organize your data (look at machine_groups_reorganized variable) and take advantages of the python dictionaries (c.f. edited answer)

the problem is that those data sets are results of mongodb aggregation functions, they are in fact cursors, I don't know how to exploit them as dicts

sure no problem, so you just have to convert them to Python dictionaries as I demonstrated to you in my answer then :) does it work for you?

I did and I keep getting this error TypeError: 'CommandCursor' object has no attribute '__getitem__'
