-1

I have a dictionary which contains some dictionaries (whose number is not fixed), which, in turn, contain lists of numbers (the number of lists of each sub-dictionary is not fixed either).

Data_Dict ={0:{0:[list 00], 1:[list 01], 2:[list 02], 3:[list 03], ...},
            1:{0:[list 10], 1:[list 11], 2:[list 12], 3:[list 13], ...},
            2:{0:[list 20], 1:[list 21], 2:[list 22], 3:[list 23], ...},
            ...}

Data_Dict is what I get from parsing data from different files, each of which provides information about different objects.

The keys of the dictionaries come from the for cycles I used to import the different files or the different objects (more specifically the keys refer to the files when the values are the dictionaries, while the keys refer to the single object when the values are the strings).

What I am trying to do is to concatenate the lists with a specific key of all the sub-dictionaries in one single list. It is important that order is kept, meaning elements of the list Data_Dict[0][0] must come before elements of the list Data_Dict[1][0], and so on. Here you have the desired outcome:

NewData_Dict ={0:[list 00, list 10, list 20,...],
               1:[list 01, list 11, list 21,...],
               2:[list 02, list 12, list 22,...], 
               ...}

I've been trying to use what was suggested in another question (How can I combine dictionaries with the same keys?), which is:

NewData_Dict = {}
for k in Data_Dict[0]:
    NewData_Dict[k] = [d[k] for d in Data_Dict]

What it gives me is:

NewData_Dict[k] = [d[k] for d in Data_Dict]
TypeError: 'int' object is not subscriptable

I tried also the answer of question Combine values of same keys in a list of dicts:

NewData_Dict = {
    k: [d.get(k) for d in Data_Dict]
    for k in set().union(*Data_Dict)
}

Getting in return:

NewData_Dict = {k: [d.get(k) for d in Data_Dict] for k in set().union(*Data_Dict)}
TypeError: 'int' object is not iterable

I think the problem might be the keys of my dictionaries being 'int'. Is my assumption correct? How can I fix this?

Jeremy
  • 1
  • 1

5 Answers5

0

This loop could be helpful

the_new_dic = {}
for key_1 in range(len(the_dict)):
  for key_2 in range(len(the_dict[key_1])):
    the_new_dic[key_1].extend(the_dict[key_2][key_1])

0

You can iterate over the items of each nested dict.

data = {0: {0: ['00', '00'], 1: ['01', '01'], 2: ['02', '02'], 3: ['03', '03']},
        1: {0: ['10', '10'], 1: ['11', '11'], 2: ['12', '12'], 3: ['13', '13']},
        2: {0: ['20', '20'], 1: ['21', '21'], 2: ['22', '22'], 3: ['23', '23']}}

new = {}
for x in data.values():
    for key in x:
        try:
            new[key].extend(x[key])
        except KeyError:
            new[key] = x[key]

Output:

>>> new
{0: ['00', '00', '10', '10', '20', '20'], 
 1: ['01', '01', '11', '11', '21', '21'], 
 2: ['02', '02', '12', '12', '22', '22'], 
 3: ['03', '03', '13', '13', '23', '23']}
alec
  • 5,799
  • 1
  • 7
  • 20
0
for i in range(len(Data_dict)):
    for j in range(len(Data_dict[i] - 1):
        Data_dict[i][0].extend(Data_dict[i][j+1]
0

Explanation in code comments. Try:

Data_Dict ={0:{0:["A", "B", "C"], 1:["E", "F", "G"], 2:["H", "I", "J"], 3:["K", "L", "M"]},
            1:{0:["N", "O", "P"], 1:["Q", "R", "S"], 2:["T", "U", "V"], 3:["W", "X", "Y"]},
            2:{0:["AA", "BB", "CC"], 1:["DD","EE","FF"], 2:["GG", "HH", "II"], 3:["JJ","KK","LL"]},}

New_Data_Dict = {}
# for each key in Data_Dict
for dict in Data_Dict:
    # for each key in Data_Dict[key]
    for list in Data_Dict[dict]:
        # if list key exists in New_Data_Dict append Data_Dict[dict key][list key] <<== Value of list
        if list in New_Data_Dict:
            New_Data_Dict[list]+=(Data_Dict[dict][list])
        # else create list key and add value
        else:
            New_Data_Dict[list] = Data_Dict[dict][list]

print(New_Data_Dict)

Output:

{0: ['A', 'B', 'C', 'N', 'O', 'P', 'AA', 'BB', 'CC'], 
 1: ['E', 'F', 'G', 'Q', 'R', 'S', 'DD', 'EE', 'FF'], 
 2: ['H', 'I', 'J', 'T', 'U', 'V', 'GG', 'HH', 'II'], 
 3: ['K', 'L', 'M', 'W', 'X', 'Y', 'JJ', 'KK', 'LL']}
Thaer A
  • 2,243
  • 1
  • 10
  • 14
0

Simple solution is here

dictionaries = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'c': 6}]

result = {}

for dictionary in dictionaries:
    for key, value in dictionary.items():
        if key in result:
            result[key].append(value)
        else:
            result[key] = [value]

print(result)
umar
  • 21
  • 5