All posts
postMay 22, 2026

Clean null values from a list — Python

#python#data-cleaning#challenge
Easy⏱️ Logic Challenge
Given a list with mixed values: [1, None, 2, "", 3, None, 0, 4], return only the truthy non-None values. Expected: [1, 2, 3, 4]
Ver solución
values = [1, None, 2, "", 3, None, 0, 4] # Keep only truthy values (drops None, "", 0) clean = [v for v in values if v] print(clean) # [1, 2, 3, 4] # If you only want to drop None (keep 0 and ""): not_null = [v for v in values if v is not None] print(not_null) # [1, 2, '', 3, 0, 4]

Real-world data is messy. Sources return None where you expected a value, empty strings where you expected text, and zero where you expected a date. Before you can transform or aggregate, you have to filter the noise.

This challenge teaches the most common cleaning pattern: filter a list to keep only valid items. The same logic applies whether you are using a list comprehension, Pandas dropna(), or SQL WHERE column IS NOT NULL.