def whitelist(d, fields):
    """Whitelists a dictionary by keeping ONLY the selected `fields`.
    Non-destructive (creates and returns a new dict)."""
    ret = type(d)()
    for f in fields:
        if f in d:
            ret[f] = d[f]
    return ret