I'm studying for my CS test right now and trying to write code to, given the entire IMDB database in plaintext, find the actors with the most films in common. I'm pretty much done, I just keep hitting a strange KeyError. Here's my code:

The problem I am encountering is, when I run it, it produces a keyerror when it runs the highlighted code. I have verified that it is indeed indexing a dictionary with a string and that said key is in the dictionary but it still isn't working. Any ideas?
When you do other_people = people_and_their_movies, you're not making a copy. You're making other_people point to the same object that people_and_their_movies points to. So when you do del other_people[actor1], you delete the actor from both dicts (because they are the same).
Instead, do other_people = people_and_their_movies.copy().
(Incidentally, on StackOverflow it's better to copy and paste your actual code as text instead of a screenshot. This way people can cut and paste your code and try it out themselves to figure out what's wrong with it.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With