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?