From 358c2cdb7e80b2835b492082b3ee26f0fb6dcc7f Mon Sep 17 00:00:00 2001 From: amerkurev Date: Sun, 14 Jan 2024 18:00:56 +0300 Subject: [PATCH] refactor: use defaultdict in group_links for cleaner code --- app/routers/links.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/routers/links.py b/app/routers/links.py index 906e560..557ce8a 100644 --- a/app/routers/links.py +++ b/app/routers/links.py @@ -2,6 +2,7 @@ import datetime import hashlib +from collections import defaultdict from operator import itemgetter from statistics import median @@ -155,12 +156,9 @@ def allowed_domain(href: str, domain: str) -> bool: def group_links(links: Sequence[Mapping]) -> dict: # group links by 'CSS selector', 'color', 'font', 'parent padding', 'parent margin' and 'parent background color' properties - links_dict = {} + links_dict = defaultdict(list) for link in links: - key = make_key(link) - if key not in links_dict: - links_dict[key] = [] - links_dict[key].append(link) + links_dict[make_key(link)].append(link) return links_dict