How to create dict from class without None fields?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When converting Python objects to dictionaries, None values often create noisy payloads and can change API behavior in unexpected ways. In some systems, sending a key with None means "clear this value," while omitting the key means "leave it unchanged." A robust solution should make that rule explicit and apply it consistently across simple and nested objects.
Pick the Right Serialization Style
There are three common patterns:
- Plain class using
__dict__ - '
dataclassusingasdict' - Model library with built-in exclude options
For lightweight projects, plain class or dataclass is enough. The main requirement is a predictable filter step that removes only None, not valid falsy values like 0, False, or an empty string.
A minimal class-based approach:
This is often good enough for flat objects.
Dataclass Version With Stronger Structure
dataclass gives clearer field definitions and better tooling support. You can still apply the same filter rule after asdict.
This keeps the implementation explicit and easy to test.
Handling Nested Objects and Collections
Most real payloads are nested. A one-level comprehension will not remove None values inside lists, dictionaries, or nested objects. For that, use a recursive cleaner.
This approach keeps nested payloads clean while preserving valid falsy values.
API Patch Semantics: Omit vs Null
Before you finalize your serializer, confirm contract semantics with API consumers.
- Omitted key means no change.
- Key present with
Nonemeans clear existing value.
If your API depends on that difference, do not blindly remove None everywhere. Sometimes you need a method that supports both modes.
One explicit flag is safer than hidden behavior.
Testing Strategy That Prevents Regressions
Create focused tests around filtering rules. Verify the following cases:
- '
Nonevalues are removed.' - '
0,False, and empty string are preserved.' - Nested dictionaries and lists are cleaned correctly.
- Optional mode that keeps nulls works as expected.
Small tests catch subtle breakage quickly when models evolve.
Common Pitfalls
- Using
if vinstead ofif v is not None, which accidentally removes0andFalse. - Cleaning only top-level keys and leaving nested
Nonevalues untouched. - Applying one global rule when different API endpoints need different null semantics.
- Mutating the original object during filtering, which creates hard-to-track side effects.
- Skipping tests for falsy values and nested collections.
Summary
- Remove
Nonevalues with an explicit filter rule, not generic truthy checks. - For flat objects,
__dict__orasdictplus a comprehension is sufficient. - For nested payloads, use a recursive cleaner that handles dicts and lists.
- Confirm API meaning of omitted keys versus null keys before finalizing behavior.
- Add tests for falsy values and nested structures to keep serialization stable.

