-1

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']
        }
    }
]
Paul M.
  • 10,481
  • 2
  • 9
  • 15
ar809
  • 81
  • 1
  • 2
  • 9
  • Maybe if there is an easier way of replacing the id from list_one using list_two id? – ar809 Jul 19 '21 at 15:51
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Jul 19 '21 at 15:55
  • I used debug here, but I don't understand what is causing these issues. I'm not used to Python and working with lists. – ar809 Jul 19 '21 at 15:57
  • What is your loop trying to do? – not_speshal Jul 19 '21 at 16:02
  • It iterates through both list so I can use the keys for validation. The last loop sets a tuple that I can use to set `id` equal to the index from list_two. This allows me to append the multiple `id` values. If I don't do enumerate it only appends one value. – ar809 Jul 19 '21 at 16:05
  • Your expected output is very confusing For "CA" and "TE", you want the id from `list_two` but for "DA" you want the id from `list_one`? What is the logic here? – not_speshal Jul 19 '21 at 16:07
  • Sorry, that was my mistake. They all should be from `list_two`. I was copying what I want the output to be and forgot to replace the id/ – ar809 Jul 19 '21 at 16:09

2 Answers2

0

You can do this with a dictionary comprehension:

result = list()
for d in list_one:
    #only keep the dictionary from list_two where the name matches
    other = [o for o in list_two if o["name"]==d["name"]][0]
    result.append({"name": d["name"], "group": {"id": other["id"]}})

>>> result
[{'name': 'CA', 'group': {'id': ['ac123', 'bb12345']}},
 {'name': 'TE', 'group': {'id': 'abc123'}},
 {'name': 'DA', 'group': {'id': 'e123'}}]
not_speshal
  • 22,093
  • 2
  • 15
  • 30
0

When the iteration encounters a list_A and list_B each with 'name':'CA'; list_B['id'] is a list with two items.

  • list_B['id'] is a list with two items

    list_two[0]['id'] --> ['ac123', 'bb12345']
    
  • listA['group']['id'] is a list with two items

    list_one[0]['group']['id'] --> ['a12345', 'aa12']
    

This part of your code iterates twice and assigns ['ac123', 'bb12345'] to each item of listA['group']['id']

                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']

It is like doing this

>>> a = [1,2]
>>> b = ['a','b']
>>> for i,item in enumerate(a):
...     print(i,item)
...     a[i] = b
...
0 1
1 2
>>> a
[['a', 'b'], ['a', 'b']]

Make this change:

            if list_B['id'] not in list_A['group']['id']:  #list_A and #list_B are a dictionary
                list_A['group']['id'] = list_B['id']
wwii
  • 23,232
  • 7
  • 37
  • 77