Problem
A data team at Acme Analytics stores raw dataset rows as Python dictionaries. Write a function to clean and preprocess these records by normalizing text fields, replacing missing values, and removing duplicate rows.
Task
Given a list of records, where each record is a dictionary with string keys and values that may be strings, numbers, or None, return a new list of cleaned records.
A record should be cleaned using these rules:
- Trim leading and trailing spaces from all string values.
- Convert empty strings (after trimming) to
"UNKNOWN". - Replace
Nonevalues with"UNKNOWN". - Convert all string values to lowercase.
- Remove duplicate records after cleaning. Two records are duplicates if they contain the same key-value pairs.
- Preserve the order of the first occurrence of each unique cleaned record.
Constraints
- 1 <= len(records) <= 10^4
- Each record contains between 1 and 20 keys
- Keys are non-empty strings
- String values have length at most 200
Function Signature
def clean_dataset(records):
You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.
