Q:

Python - Join two lists of dictionaries on a contains, and leave out any that do not match?

Im trying to join two list of dictionaries where one of the fields contains a section of another field, and if there isn't a match to lave it out.
list_1 = [{
    "bytes_in": 0,
    "rsrq": -50,
    "hostname": "AB9472"
},
{
    "bytes_in": 0,
    "rsrq": -90,
    "hostname": "DF4845"
},
{
    "bytes_in": 0,
    "rsrq": "None",
    "hostname": "FC4848"
}
]

list_2 = [{
    "id": 249,
    "ref_no": "AB9472 - 015632584",
    "rssi": "-75.0",
    "circuit_type": "4G"
},
{
    "id": 17,
    "ref_no": "DF4845 - 8984494",
    "rssi": "-20.0",
    "circuit_type": "4G"
}]

so in the above example list_1 hostname field would be contained inside list_2 ref_no and would match for the first two records and then be created in a new list_3
I feel I can do this in iterrtools but I'm not sure how?
Thanks 
desired output:
list_3 = [{
    "id": 249,
    "ref_no": "AB9472 - 015632584",
    "rssi": "-75.0",
    "circuit_type": "4G",
    "bytes_in": 0,
    "rsrq": -50,
    "hostname": "AB9472"
},
{
    "id": 17,
    "ref_no": "DF4845 - 8984494",
    "rssi": "-20.0",
    "circuit_type": "4G",
    "bytes_in": 0,
    "rsrq": -90,
    "hostname": "DF4845"
}]

A:

Try this :
list_3 = []
for i in list_1:
    for j in list_2:
        if i['hostname'] == j['ref_no'].split("-")[0].strip():
            list_3.append({**i,**j})

print(list_3)

