-5

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.

2
  • 1
    Please don't use unrelated tags in your question. json and list are not useful, instead use tags like comparison that are connected with your problem. Commented Jul 21, 2024 at 18:24
  • You might have been trying for days, but you posted exactly zero attempts. Commented Jul 21, 2024 at 20:42

2 Answers 2

2
#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
Sign up to request clarification or add additional context in comments.

Comments

1

this should do

def get_set(matches):
    return set([match_['Match Full'] for match_ in matches])
    
pre_set= get_set(pre)
live_set = get_set(live)
print(pre_set-live_set) # {'Parma v Fiorentina'}

1 Comment

Thank you! This works, is there any way to do it via simple for loops? Could you kindly give me an explanation to all this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.