Skip to content

script_9.py - List Deduplication

Code

See script_9.py for full code.

Explanation

Line 17: unique_unordered = list(set(emails)) - set() converts list to set type - Sets automatically remove duplicates - Sets are unordered (lose original order) - list() converts back to list - Fast O(n) operation

Line 22: unique_ordered = list(dict.fromkeys(emails)) - dict.fromkeys() creates dict with emails as keys - Dict keys are unique (duplicates ignored) - Dict preserves insertion order (Python 3.7+) - .fromkeys() is class method taking iterable - list() extracts the keys - Preserves order while removing duplicates

Line 27-30: Manual deduplication - Loop through each email - if email not in unique_manual: checks membership - .append() only if not already present - Preserves order, explicit logic

Line 38: unique_users = list({user["id"]: user for user in users}.values()) - Dict comprehension: {key: value for item in iterable} - Uses id as key (ensures uniqueness) - Later occurrences overwrite earlier ones - .values() extracts the dict values (user dicts) - Converts to list

Line 44: from collections import Counter - Counter is specialized dict for counting

Line 45: email_counts = Counter(emails) - Creates Counter object mapping email -> count - Automatically counts occurrences - Returns dict-like object

Line 46: duplicates = {email: count for email, count in email_counts.items() if count > 1} - Dict comprehension with filter - .items() returns (key, value) pairs - Only includes emails with count > 1