I got this loop where it iterates through list_one that contains the key "id" nested inside "groups". The other list_two also contains the key "id" but is not nested. The first thing I do is to validate if they are equal to each other. If not then proceed to loop.
The problem I'm encountering is that for new_list after appending, CA
ids are the same multiple times. They also were appended inside another array and prints multiple times.
list_one = [
{
'name': 'CA',
'group':
{
'id': ['a12345', 'aa12']
}
},
{
'name': 'TE',
'group':
{
'id': ['b12345']
}
},
{
'name': 'DA',
'group':
{
'id': ['cab124']
}
}
]
list_two = [
{
'name': 'CA',
'id': ['ac123', 'bb12345']
},
{
'name': 'TE',
'id': 'abc123'
},
{
'name': 'DA',
'id': 'e123'
}
]
for list_A in list_one: # list_one is a list
for list_B in list_two: # list_two is a list
if list_A['name'] == list_B['name']:
if list_B['id'] not in list_A['group']['id']: #list_A and #list_B are a dictionary
for index, ids in enumerate(list_A['group']['id']): # using tuple to assign the values using the index
list_A['group']['id'][index] = list_B['id']
new_list.append(list_A)
Output I get:
[
{
'name': 'CA',
'group': {
'id':
[
['ac123', 'bb12345'],
['ac123', 'bb12345']
]
}
},
{
'name': 'TE',
'group': {
'id': [
['abc123']
]
}
},
{
'name': 'DA',
'group': {
'id': [
['e123']
]
}
}
]
Expected output:
[
{
'name': 'CA',
'group': {
'id': ['ac123', 'bb12345']
}
},
{
'name': 'TE',
'group': {
'id': ['abc123']
}
},
{
'name': 'DA',
'group': {
'id': ['e123']
}
}
]