In Part 3, you first learn the cleaning pattern with one tiny dictionary, then apply the same logic to a real CSV dataset as an exercise.
You will learn this exact sequence:
- create data with a missing value
- detect what is missing
- update/fix the missing value
- save cleaned data to disk
Then you will apply the same flow on studio_ghibli_movies.csv.
Create:
session2/solutions/exercise-02-02.pyStart with two records:
- one missing
music_by - one missing
year
movies = [
{
"title": "Howl's Moving Castle",
"year": "2004",
"director": "Hayao Miyazaki",
"music_by": "",
},
{
"title": "Kiki's Delivery Service",
"year": "",
"director": "Hayao Miyazaki",
"music_by": "Joe Hisaishi",
},
]for i, movie in enumerate(movies, start=1):
for key, value in movie.items():
if value.strip() == "":
print(f"Record {i} missing field: {key}")Tip
strip() is a Python string method that removes extra characters from the beginning and end of a string. For example text = " hello " with text.strip() it becomes hello. Use the strip as a general rule of thumb when working with text.
Expected output:
Record 1 missing field: music_by
Record 2 missing field: yearTip
What are the time and space complexities of this script?
Show answer
Time: O(n * m), where n is number of records and m is number of fields per record.
Space: O(1) extra space.
Can you do it better for this specific dataset? Yes.
If you already know the fields you care about (year, music_by), you can avoid looping through every key:
for i, movie in enumerate(movies, start=1):
if movie["music_by"].strip() == "":
print(f"Record {i} missing field: music_by")
if movie["year"].strip() == "":
print(f"Record {i} missing field: year")Tip
What are the time and space complexities of this improved script?
Show answer
Time: O(n), because each record is checked once with constant work.
Space: O(1) extra space.
if movies[0]["music_by"].strip() == "":
movies[0]["music_by"] = "Joe Hisaishi"
if movies[1]["year"].strip() == "":
movies[1]["year"] = "1989"
print(movies)This is the core cleaning idea: detect then update.
Tip
What are the time and space complexities of this script?
Show answer
Time: O(1) for this exact two-record example.
Space: O(1) extra space.
In a generalized loop across n records, update time becomes O(n).
Sometimes we want both versions:
- raw/original data (unchanged)
- cleaned data (updated)
Naming note:
original_movies: backup copy for safekeeping (do not edit this)cleaned_movies: working copy that we edit during cleaning
original_movies = [movie.copy() for movie in movies]
cleaned_movies = [movie.copy() for movie in movies]
if cleaned_movies[0]["music_by"].strip() == "":
cleaned_movies[0]["music_by"] = "Joe Hisaishi"
if cleaned_movies[1]["year"].strip() == "":
cleaned_movies[1]["year"] = "1989"
print("Original:", original_movies)
print("Cleaned:", cleaned_movies)From this point onward, "cleaned data" means the cleaned_movies list.
Tip
What are the time and space complexities of this script?
Show answer
Time: O(n * m), because each copied record copies its fields.
Space: O(n * m), because we store two copied lists.
Use JSON for this mini example:
import json
with open("movies_clean.json", "w", encoding="utf-8") as file:
json.dump(cleaned_movies, file, ensure_ascii=False, indent=2)
print("Saved: movies_clean.json")Now you have completed the full cleaning pipeline.
Tip
What are the time and space complexities of this script?
Show answer
Time: O(n * m), because writing depends on number of records and fields.
Space: O(1) extra space (excluding data already in memory).
Use: Birkbeck/studio_ghibli_movies
Download:
hf download Birkbeck/studio_ghibli_movies studio_ghibli_movies.csv \
--repo-type dataset \
--local-dir .Expected file in session2/:
studio_ghibli_movies.csvIn session2/solutions/exercise-02-02.py, do the following:
-
Load the
studio_ghibli_movies.csvdataset withcsv.DictReader. -
Find missing values by column and print where they are (line number + movie title).
-
Fix missing values (for this dataset, check
yearandmusic_by). -
Calculate:
- average of
year - how many times Miyazaki appears as director
- average of
-
Save cleaned rows into a new file:
studio_ghibli_movies_clean.csv
-
Re-check missing values after cleaning and print remaining missing count.
-
What are the time and space complexities of your full script?
Complete the following quiz:
quizmd quizzes/python-dict-cleaning-theory-coding-quiz.md