sasquatchphd
sasquatchphd

Reputation: 15

Iterate through nested dictionaries to find differences and commonalities

I have run into trouble with a lesson I'm working through. I have this data:

amandas_data = {
    "subscriptions": ["Service A", "Service B"],
    "watched": [{ "title": "Title A" }, { "title": "Title B" }],
    "friends": [
        {
            "watched": [
                {
                    "title": "Title A",
                    "host": "Service A"
                },
                {
                    "title": "Title C",
                    "host": "Service C"
                }
            ]
        },
        {
            "watched": [
                {
                    "title": "Title A",
                    "host": "Service A"
                },
                {
                    "title": "Title B",
                    "host": "Service B"
                },
                {
                    "title": "Title D",
                    "host": "Service D"
                }
            ]
        }
    ]
}

I need to find which titles exist in the "friends" nested "watched" dictionaries—but not in amandas_data's "watched"—and also share "host" values with amandas_data's "subscriptions" list.

I haven't even been able to get it to decide which ones "friends" have but amandas_data "watched" doesn't. This is as far as I got.

def get_available_recs(user_data):
    rec_list = []
    friend_data = user_data["friends"]
    user_watched = user_data["watched"]
    user_subs = user_data["subscriptions"]
    for f in friend_data:
        for m in f["watched"]:
            if m["title"] not in user_watched:
                rec_list.append(m)
    print(rec_list)

What do I do?

Upvotes: 1

Views: 50

Answers (3)

Farhan Sangaji
Farhan Sangaji

Reputation: 146

friends_watched = [a for w in amandas_data["friends"] for a in w["watched"]]
not_watched_by_amanda = [nw for nw in friends_watched if nw["title"] not in [a["title"] for a in amandas_data["watched"]]]
print(not_watched_by_amanda)

This will get the title and host of friends_watched

Upvotes: 1

John Gordon
John Gordon

Reputation: 33335

# iterate over each friend
for friend in amandas_data['friends']

    # iterate over this friend's watched shows
    for watched in friend['watched']:

        # if amanda also has this service
        if watched['host'] in amandas_data['subscriptions']:

            # if amanda has not watched this show
            if watched['title'] not in [show['title'] for show in amandas_data['watched']:

                print('amanda has not watched ', watched['title'])

Upvotes: 0

fsl
fsl

Reputation: 3280

IIUC, it's always easier to break down your problem into parts:

exceptions = set(i["title"] for i in amandas_data["watched"])

friends_titles = set()
for obj in amandas_data["friends"]:
     for inner in obj["watched"]:
         friends_titles.add(inner["title"])

target = friends_titles.difference(exceptions)

Output:

In [155]: friends_titles.difference(exceptions)
Out[155]: {'Title C', 'Title D'}

PS: I'm using set to handle duplicates, but it'd work similarly using lists if you desire.

Upvotes: 0

Related Questions