'Python: Best way to check if a key value match exists between two dictionaries?
edit
I have 2 list of dictionaries and I am trying to compare the value of a key in the dictionaries list_1 to see if it that value exists in the value of a different key in the dictionaries list_2 as I iterate through list_2.
Lists of Dictionaries
list_1: {'Video ID': 12894, 'Title': 'Title 1'...}, {'Video ID': 128993, 'Title': 'Title 3'...}, {...}
list_2: ('post_id': 12894, 'title': 'Title 2'...,} {'post_id': 128993, 'title': 'Title 5'...,}, {...}
I would like to see if value of key Video ID in the dictionaries of list_1 match the value of the key post_id in the dictionaries of list_2.
I am trying the code below from a post here, but I am getting confused on using the keys to access the correct value.
Code
for s in dictionary_2:
s = list(s.values())
for f in dictionary_1:
f = list(f.values())
if f['Video Id'] in s and f['post_id'] in s:
print(s)
Solution 1:[1]
The Best way depends on what you're trying to achieve.
If you are looking for the most efficient solution given your context then Solution 1 is probably the way to go since you will probably have a lot of Video ID's and post_id's.
However I'm putting my original solution as Solution 2 as it could be helpful in understanding what's happening. I'm also putting Solution 3 if you want to better understand how to adapt this answer
Here is the example data that I'm using for both solutions:
list_1 = [
{'Video ID': 12894, 'Title': 'Title 1'},
{'Video ID': 128993, 'Title': 'Title 3'},
{'Video ID': 1233, 'Title': 'Title 4'}
]
list_2 = [
{'post_id': 12894, 'title': 'Title 2'},
{'post_id': 128993, 'title': 'Title 5'},
{'post_id': 1235, 'title': 'Title 6'}
]
Solution 1 (Creating a lookup table)
Credits to @JonClements
As @JonClements correctly pointed out the best solution (given your context) might be to create a lookup table by converting the post_id's in list_2 to a dictionary with all the post_id's as keys.
- Create the lookup table by adding each
post_idas the key and each dictionary inlist_2as the value. - Iterate over each
videodictionary inlist_1 - if the
Video IDof that current dictionary exists in the lookup table - Assign the matching
post_idto amatchvariable that contains the dictionary of the matching post - Check if the title of the matched post is the same as the video's title
- If they are not the same then update the
Title
# Create a lookup table
post_lookup = {d['post_id']: d for d in list_2}
# Iterate over video list and print relevant matches
for video in list_1:
if video['Video ID'] in post_lookup:
match = post_lookup[video['Video ID']]
print(f"Same ID {video['Video ID']} and {match}")
if video['Title'] != match['title']:
print(f"Outdated Title {video['Title']} and {match}")
# do something to update...
else:
print('Titles match, do nothing...')
# this else may not be necessary
else:
print(f"{video['Video ID']} Not found...")
Solution 2 (using 2 for loops and range() function)
If you simply want to compare the Video ID of list_1 to every single post_id in list_2 (disregarding efficiency) then you could do the following:
- Use the
range()function to iterate over every dictionary inlist_1 - Use the same function to iterate over every dictionary in
list_2 - Check if the current
Video IDof the current dictionary inlist_1matches the 'post_id' of the current dictionary inlist_2
for video in range(len(list_1)):
for post in range(len(list_2)):
if(list_2[post]['post_id'] == list_1[video]['Video ID']):
print(f"Video ID:{list_1[video]['Video ID']} matches post_id: {list_2[post]['post_id']}")
if(list_2[post]['title'] != list_1[video]['Title']):
print('Titles do not match, please update...')
# do something to update the video/post title
else:
print("Title's match, do nothing...")
# do something else if the titles match
Solution 3 (Adapted from Answer)
- Iterate over each dictionary in
list_2 - get the
post_idfor that current dictionary - iterate over each dictionary in
list_1 - if the
id's match and thetitle's don't then print the item and do something to update the title
for dictionary_2 in list_2:
s = dictionary_2['post_id']
for dictionary_1 in list_1:
f = dictionary_1['Video ID']
if f == s and dictionary_2['title'] != dictionary_1['Title']:
print(s)
# do something to update titles
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
