31 Years of Python | 48 Hour Sale Extension!!!
days
hours
minutes
seconds

Cleaning and Preparing Data in Python- 8/10- List not showing up and unexpected Function results

Screen Link:

My Code:

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,"")
    return string

stripped_test_data = ['1912', '1929', '1913-1923',
                      '1951', '1994', '1934',
                      '1915', '1995', '1912',
                      '1988', '2002', '1957-1959',
                      '1955', '1970', '1990-1999']

def process_date(data):
    processed_test_data = []
    for date in data:
        if '-' not in date:
            processed_test_data.append(int(date))
        if '-' in date:
            split_date = date.split('-')
            if split_date[0] == '' and split_date[1] == '':
                continue
            elif split_date[0] == '':
                split_date[1] = int(split_date[1])
                processed_test_data.append(split_date)
            elif split_date[1] == '':
                split_date[0] = int(split_date[1])
                processed_test_data.append(split_date)
            else:
                split_date = round(((int(split_date[0])) + (int(split_date[1])))/2)
                processed_test_data.append(split_date)
    return processed_test_data
   

print(process_date(stripped_test_data))

for row in moma:
    dates = row[6]
    stripped_data = strip_characters(dates)
    processed_data = process_date(stripped_data)
    row[6] = processed_data
print(moma[1][6])


What I expected to happen:
I expected the stand-alone process_data function to return the processed_test_data list, and the for loop to return the processed year for each painting in the moma database.

What actually happened:

[1912, 1929, 1918, 1951, 1994, 1934, 1915, 1995, 1912, 1988, 2002, 1958, 1955, 1970, 1994]
[1, 9, 7, 8]

When I try submitting, I get a message that “processed_test_data isn’t defined in your code, but we expected it to be list type”.
Additionally, the values in the moma loop are being broken up into four values, as opposed to being one value.

data is being passed into the function as follows

stripped_data is a string. It’s not a list.

If I gave you the following:

a = "hello"
for i in a:
    print(i)

What would you get as output? Compare that to your code above and see if that clarifies things or not. Feel free to ask more questions.

I would also recommend reading the instructions again to see if your code for process_date() is matching up with them or not. Especially focus on when and where they ask us to loop over any data.

Thank you! That clarified my issues!