I know there are a million questions like this, I just can't find an answer that works for me.
I have this:
list1 = [{'assembly_id': '1', 'asym_id_list': ['A', 'B', 'E', 'G', 'H']}, {'assembly_id': '1', 'asym_id_list': ['C', 'D', 'F', 'I', 'J']}, {'assembly_id':2,'asym_id_list':['D,C'],'auth_id_list':['C','V']}]
if the assembly_ids are the same, I want to combine the other same keys in the dict.
In this example, assembly_id 1 appears twice, so the input above would turn into:
[{'assembly_id': '1', 'asym_id_list': ['A', 'B', 'E', 'G', 'H','C', 'D', 'F', 'I', 'J']},{'assembly_id':2,'asym_id_list:['D,C'],'auth_id_list':['C','V']}]
In theory there can be n assembly_ids (i.e. assembly 1 could appear in the dict 10 or 20 times, not just 2) and there can be up to two other lists to combine (asym_id_list and auth_id_list).
I was looking at this method:
new_dict = {}
assembly_list = [] #to keep track of assemblies already seen
for dict_name in list1: #for each dict in the list
if dict_name['assembly_id'] not in assembly_list: #if the assembly id is new
new_dict['assembly_id'] = dict_name #this line is wrong, add the entry to new_dict
assembly_list.append(new_dict['assembly_id']) #append the id to 'assembly_list'
else:
new_dict['assembly_id'].append(dict_name) #else if it's already seen, append the dictionaries together, this is wrong
print(new_dict)
The output is wrong:
{'assembly_id': {'assembly_id': 2, 'asym_id_list': ['D,C'], 'auth_id_list': ['C', 'V']}}
But I think the idea is right, that I should open a new list and dict, and if not seen before, append; whereas if it has been seen before...combine? But it's just the specifics I'm not getting?
dict_nameto that list, ifdict_name['assembly_list']was not seen before and you can just add the lists'asym_id_list'and'auth_id_list'if it was seen before.'assembly_id':2,'asym_id_list':['D,C']to be separate strings like this:'assembly_id':2,'asym_id_list':['D', 'C']. Also in the 'assembly_id' keys you have a mixture of strings and ints (i.e.'1'and2). Although that will work, I am guessing that you did not intend the keys to be a mixture ofints andstrings