Home > OS >  How to merge dictionary with some lists from list
How to merge dictionary with some lists from list

Time:09-17

I have a list of dictionaries:

dictionary_list = [{'a':'string','b':'string','c':['string1','string2']},{'a':'string','b':'string','c':['string1','string2', 'string3']}]

and want to have all dict from list in one dictionary, but when I:

all_dict = {}
for d in dictionary_list:
    all_dict.update(d)
print(all_dict)

result is:

{'a':'string','b':'string','c':['string1','string2'],'a':'string','b':'string','c':['string1','string2', 'string3']}

could You help :)

CodePudding user response:

I think you're trying to merge the list values which you could do like this:

dictionary_list = [{'usterki_usuniete': 'some_string',
                    'pictures_usuniete': ['KATO152_AWARIA_1', 'KATO152_AWARIA_2', 'KATO152_AWARIA_3', 'KATO152_AWARIA_4', 'KATO152_AWARIA_7'],
                    'usterki_nieusuniete': 'some_string',
                    'pictures_nieusuniete': ['KATO152_AWARIA_5', 'KATO152_AWARIA_6']},
                   {'usterki_usuniete': 'some_string',
                    'pictures_usuniete': ['OPO1512_AWARIA_5'],
                    'usterki_nieusuniete': 'some_string',
                    'pictures_nieusuniete': ['OPO1512_AWARIA_1', 'OPO1512_AWARIA_2', 'OPO1512_AWARIA_3', 'OPO1512_AWARIA_4']}]

output = {}

for d in dictionary_list:
    for k, v in d.items():
        if isinstance(v, list):
            output.setdefault(k, []).extend(v)
        else:
            output[k] = v

print(output)

Output:

{'usterki_usuniete': 'some_string', 'pictures_usuniete': ['KATO152_AWARIA_1', 'KATO152_AWARIA_2', 'KATO152_AWARIA_3', 'KATO152_AWARIA_4', 'KATO152_AWARIA_7', 'OPO1512_AWARIA_5'], 'usterki_nieusuniete': 'some_string', 'pictures_nieusuniete': ['KATO152_AWARIA_5', 'KATO152_AWARIA_6', 'OPO1512_AWARIA_1', 'OPO1512_AWARIA_2', 'OPO1512_AWARIA_3', 'OPO1512_AWARIA_4']}

Note:

The problem with this could be when (if?) the values for keys such as usterki_nieusuniete are different in each sub-dictionary. With this code it would always be the last observed value which may not be what you want

CodePudding user response:

You need to provide keys for your dictionary, for example:

dictionary_list = [{'usterki_usuniete': 'some_string', 'pictures_usuniete': ['KATO152_AWARIA_1', 'KATO152_AWARIA_2', 'KATO152_AWARIA_3', 'KATO152_AWARIA_4', 'KATO152_AWARIA_7'], 'usterki_nieusuniete': 'some_string', 'pictures_nieusuniete': ['KATO152_AWARIA_5', 'KATO152_AWARIA_6']}, {'usterki_usuniete': 'some_string', 'pictures_usuniete': ['OPO1512_AWARIA_5'], 'usterki_nieusuniete': 'some_string', 'pictures_nieusuniete': ['OPO1512_AWARIA_1', 'OPO1512_AWARIA_2', 'OPO1512_AWARIA_3', 'OPO1512_AWARIA_4']}]

all_dict = {}
i = 0
for j in dictionary_list:
    all_dict[i] = j
    i =1
all_dict 

Output:

{0: {'usterki_usuniete': 'some_string',
  'pictures_usuniete': ['KATO152_AWARIA_1',
   'KATO152_AWARIA_2',
   'KATO152_AWARIA_3',
   'KATO152_AWARIA_4',
   'KATO152_AWARIA_7'],
  'usterki_nieusuniete': 'some_string',
  'pictures_nieusuniete': ['KATO152_AWARIA_5', 'KATO152_AWARIA_6']},
 1: {'usterki_usuniete': 'some_string',
  'pictures_usuniete': ['OPO1512_AWARIA_5'],
  'usterki_nieusuniete': 'some_string',
  'pictures_nieusuniete': ['OPO1512_AWARIA_1',
   'OPO1512_AWARIA_2',
   'OPO1512_AWARIA_3',
   'OPO1512_AWARIA_4']}}
  • Related