Search code examples
pythondictionarycomparison

Compare python dictionaries and find missing elements


I'm encountering a problem, I've been trying for days but without getting any results. I would like to compare 2 dictionaries, in one dictionary there are "Pre match" football matches and in the second dictionary there are "Live" football matches.

I would like to compare them with each other and print them if there are no pre-match games live.

EXAMPLE 1

pre = [{
        "Home": "Genoa",
        "Away": "Inter",
        "Match Full": "Genoa v Inter",
        "Start Data": "19 Lug",
        "Start Time": "21:30"
    },
    {
        "Home": "Parma",
        "Away": "Fiorentina",
        "Match Full": "Parma v Fiorentina",
        "Start Data": "17 Ago",
        "Start Time": "18:30"
    }]

live = [{
        "Home": "Dagon Star United FC",
        "Away": "Ispe FC",
        "Match Full": "Dagon Star United FC v Ispe FC"
    },
    {
        "Home": "Genoa",
        "Away": "Inter",
        "Match Full": "Genoa v Inter"
    }]

check = [[x for x in pre if x['Match Full'] != i['Match Full']] for i in live]

print(check)

I am not receiving the desired result, I also tried the following code, without receiving the right result.

EXAMPLE 2

pre = [{
        "Home": "Genoa",
        "Away": "Inter",
        "Match Full": "Genoa v Inter",
        "Start Data": "19 Lug",
        "Start Time": "21:30"
    },
    {
        "Home": "Parma",
        "Away": "Fiorentina",
        "Match Full": "Parma v Fiorentina",
        "Start Data": "17 Ago",
        "Start Time": "18:30"
    }]

live = [{
        "Home": "Dagon Star United FC",
        "Away": "Ispe FC",
        "Match Full": "Dagon Star United FC v Ispe FC"
    },
    {
        "Home": "Genoa",
        "Away": "Inter",
        "Match Full": "Genoa v Inter"
    }]

for x in pre:
    for i in live:
        if x['Match Full'] != i['Match Full']:
            print(x['Match Full'])

What I'm trying to get is only the missing pre-match in the "live" dict, in this case it should only print "Parma v Fiorentina" as it is missing in the dictionary

Any solution will be appreciated, thank you in advance.


Solution

  • #Create a list of value of 'Match Full' from live
    live_lst = [x["Match Full"] for x in live] 
    
    for x in pre:
        if x["Match Full"] not in live_lst:
            print(x["Match Full"])
    
    #Output : Parma v Fiorentina