LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

List is shorter than expected error

Hello,
I had a question about the 7th page of the “Cleaning and Preparing Data in Python” Mission.

URL: Learn data science with Python and R projects

My Code:

def strip_characters(date):
    for b_char in bad_chars:
        clean_date = date.replace(b_char,"")
    return clean_date

stripped_test_data = []
for date in test_data:
    clean_test_data = strip_characters(date)
    stripped_test_data.append(clean_test_data)

It is saying that the value of the stripped_test_data is not what was expected. I don’t know what I’m doing wrong here. I used different variable names, however, they (I think!) seem to be in the correct places when I compare them to the answer from Dataquest.

For every iteration of the loop, you store a new date in clean_date. After the loop ends, clean_date will only store the last change that was made to date.

You need to save the changes back into date -

date = date.replace(b_char,"")

and then return date.

2 Likes

Hi @the_doctor,
Thanks!