36. Reformat Date Strings
Difficulty: Easy · Topic: Data wrangling
A form collects dates as day/month/year text, like "01/03/2026" for 1 March 2026. Write to_iso(dates): return a list with each date in ISO format ("2026-03-01"), in the same order.
- Ignore spaces around a value.
- Anything that is not a real date in day/month/year form (31 February, month 13, a different format, an empty string) becomes
Nonein the output, so one bad value does not stop the rest.
Example: to_iso(['01/03/2026', '25/12/2025']) gives ['2026-03-01', '2025-12-25'].
Your code runs in CPython on our servers against 5 tests, including hidden ones.
Sample test
to_iso(['01/03/2026', '25/12/2025'])Expected: ['2026-03-01', '2025-12-25']
Hints
Hint 1
datetime.strptime(text, "%d/%m/%Y") reads the text and raises ValueError when it does not fit or is not a real date. Careful: %M is minutes, %m is the month.Hint 2
.date().isoformat() writes YYYY-MM-DD.Hint 3
CatchValueError per value and append None.Learn the concepts
- Files, JSON, CSV and dates · 8 min read. Read files line by line, JSON and CSV, ISO dates, strptime codes, time zones and zoneinfo.
More Python problems
- Parse Log Lines into Records · Easy
- CSV Rows to Typed Records · Easy
- Flatten Nested JSON · Medium
- Keep the Latest Record per Key · Medium
- Group and Aggregate without pandas · Medium
- Left Join Two Lists of Dicts · Medium
- Merge Overlapping Sessions · Hard
- Schema Validation with a Dead-Letter Queue · Hard