0

I have a list of dictionary as follows:

knownShows = [
    {'Parsed Name': 'A', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'B', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'C', 'Parsed Season':'2', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'D', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''}
]

And I am using a python script to automatically add new dictionaries (show name with parsed season number) based on the episode name (eg "A - s01e02" becomes Parsed Name "A", and Parsed Season "1").

Is there any way to check if a certain combination of key/value pairs already exists in a dictionary in the list?

I was trying to use a modified version of this solution, turning it into

if not (any(d['Parsed Name'] == showName for d in knownShows) and any(d['Parsed Season'] == str(season) for d in knownShows)):

but this has started causing problems when there are multiple seasons of the same show in the list being parsed.

for example, I have an episode with the show/season combination of:

'Parsed Name' = "A"
'Parsed Season' = "2"

Since both: a show called 'A' and a season '2' already exist in the list, it isnt creating a new entry for Season 2 of A.

Is there any way to change the if statement to check for the combination of Show A and Season 2, and only turn False if this specific combination exists inside the same dictionary inside the list?

amanmore
  • 15
  • 4

3 Answers3

2

You might use AND inside list comprehension the following way:

if not any((d['Parsed Name'] == showName and d['Parsed Season'] == str(season)) for d in knownShows):
MikhailShi
  • 36
  • 3
0

You can use list comprehension to merge the the name and season to a single field and check that.

knownShows = [
    {'Parsed Name': 'A', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'B', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'C', 'Parsed Season':'2', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'D', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''}
]

def entryexists(name, season):
    s = [d['Parsed Name']+'|'+d['Parsed Season'] for d in knownShows]
    if name+'|'+season in s:
        return True
    return False
    

print(entryexists('A','1'))
print(entryexists('D','2'))

Output

True
False
Mike67
  • 11,175
  • 2
  • 7
  • 15
0

If you have control of the data structures you may want to consider storing the data in a dictionary instead of a list:

knownShows = [
    {'Parsed Name': 'A', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'B', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'C', 'Parsed Season':'2', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'D', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''}
]

# build the keyed dictionary
keyed_known_shows = {}
for d in knownShows:
    key = (d["Parsed Name"], d["Parsed Season"])
    keyed_known_shows[key] = d
    

That way you have a direct hash to the desired entry, and don't have to iterate over your data.

new_key = ("A", "1")
new_key in keyed_known_shows

True

monkut
  • 42,176
  • 24
  • 124
  • 155