Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to compare two 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 if there are no pre-match games live you must print them, now I will show the code better

EXAMPLE 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`import json

pre = [{
        "Sport": "Calcio",
        "Championship": "Italia - Serie A",
        "Home": "Genoa",
        "Away": "Inter",
        "Match Full": "Genoa v Inter",
        "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355896/F3/I0/",
        "Start Data": "19 Lug",
        "Start Time": "21:30"
    },
    {
        "Sport": "Calcio",
        "Championship": "Italia - Serie A",
        "Home": "Parma",
        "Away": "Fiorentina",
        "Match Full": "Parma v Fiorentina",
        "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355904/F3/I0/",
        "Start Data": "17 Ago",
        "Start Time": "18:30"
    }]

live = [{
        "Sport": "Calcio",
        "Championship": "Myanmar - Lega Nazionale",
        "Home": "Dagon Star United FC",
        "Away": "Ispe FC",
        "Match Full": "Dagon Star United FC v Ispe FC"
    },
    {
        "Sport": "Calcio",
        "Championship": "Italia - Serie A",
        "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

`import json

pre = [{
        "Sport": "Calcio",
        "Championship": "Italia - Serie A",
        "Home": "Genoa",
        "Away": "Inter",
        "Match Full": "Genoa v Inter",
        "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355896/F3/I0/",
        "Start Data": "19 Lug",
        "Start Time": "21:30"
    },
    {
        "Sport": "Calcio",
        "Championship": "Italia - Serie A",
        "Home": "Parma",
        "Away": "Fiorentina",
        "Match Full": "Parma v Fiorentina",
        "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355904/F3/I0/",
        "Start Data": "17 Ago",
        "Start Time": "18:30"
    }]

live = [{
        "Sport": "Calcio",
        "Championship": "Myanmar - Lega Nazionale",
        "Home": "Dagon Star United FC",
        "Away": "Ispe FC",
        "Match Full": "Dagon Star United FC v Ispe FC"
    },
    {
        "Sport": "Calcio",
        "Championship": "Italia - Serie A",
        "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.

print(x[‘Match Full’])

missing matches

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading