From 1c133d5866b95dde34f0f80a798a26ee670ade99 Mon Sep 17 00:00:00 2001 From: Anmol Deep Date: Sat, 12 Oct 2024 08:14:44 +0530 Subject: [PATCH 1/2] Fixed: KeyError caused due to the same element getting removed from a set twice for item_a in set_a: for item_b in set_b: if : set_a.remove(item_a) As you can see, if there are multiple items in set_b for which the condition evaluates to True, set_a.remove(item_a) will run twice, which will cause KeyError for second time onwards. To solve this problem, I am checking whether the item_a is there in the set_a before removing. If it is, then remove, if it isn't then continue without removing as we have already removed it --- piplicenses.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/piplicenses.py b/piplicenses.py index b2e09ef..c3b9fd8 100755 --- a/piplicenses.py +++ b/piplicenses.py @@ -435,6 +435,8 @@ def case_insensitive_partial_match_set_diff(set_a, set_b): for item_a in set_a: for item_b in set_b: if item_b.lower() in item_a.lower(): + if item_a not in uncommon_items: + continue uncommon_items.remove(item_a) return uncommon_items From 66dc1e6d0c2962c3dc8726b28719de7a050b7eee Mon Sep 17 00:00:00 2001 From: Anmol Deep Date: Mon, 14 Oct 2024 09:48:46 +0530 Subject: [PATCH 2/2] Replaced element check logic with built in set.discard method --- piplicenses.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/piplicenses.py b/piplicenses.py index c3b9fd8..bbd3285 100755 --- a/piplicenses.py +++ b/piplicenses.py @@ -435,9 +435,7 @@ def case_insensitive_partial_match_set_diff(set_a, set_b): for item_a in set_a: for item_b in set_b: if item_b.lower() in item_a.lower(): - if item_a not in uncommon_items: - continue - uncommon_items.remove(item_a) + uncommon_items.discard(item_a) return uncommon_items