Good eveneing, I want to merge two lists of dictonaries if they have the same key:value in it. Its like a join in SQL. I'm not allowed to import any modules for this problem.
Here an example:
Input:
>>> series = [
... {'s_id': 'bb', 'title': 'Breaking Bad'},
... {'s_id': 'bcs', 'title': 'Better Call Saul'}]
>>> series_characters = [
... {'c_id': 'ww', 's_id': 'bb'},
... {'c_id': 'sw', 's_id': 'bb'},
... {'c_id': 'sg', 's_id': 'bb'}
... {'c_id': 'sg', 's_id': 'bcs'}
Output should be a list of dicts with both infomration inside it:
out= [
{'s_id': 'bb', 'title': 'Breaking Bad', 'c_id': 'ww'},
{'s_id': 'bcs', 'title': 'Better Call Saul', 'c_id': 'sg'}]
I tried somthing like that but I think that my thoughts are to complicated and the code doesen't work:
def _join(tbl1, tbl2):
"""
Helping function to merge two tables inform of a list
Argurments:
tbl1 (list): list of dict's containung a table
tbl2 (list): list of dict's containung a table
Returns:
back_lst (list): list, containing wanted rows of the table
"""
back_lst = []
for element1 in tbl1:
for element2 in tbl2:
for key in tbl1[element1]:
if tbl1[element1[key]] == tbl2[element2[key]]:
a = tbl1[element1].copy()
a.update(tbl2[element2])
back_lst.append(a)
return back_lst
It would be nice to get some help here. Thanks in advance.