Function to clean up data

Hi I need your expertise on this. I tried to clean up the dash ‘-’ from this data
stripped_test_data = ['1912', '1929', '1913-1923', '1951', '1994', '1934', '1915', '1995', '1912', '1988', '2002', '1957-1959', '1955', '1970', '1990-1999']
by writing the below function:

`def process_date(string):

for str in string: #iterate over string
    if '-' in str:
      years = str.split("-") #split the string at the dash, for example '1991-1993' split into '1991', '1993'
      result = list(map(int,years)) #convert string into integer
      value = round(sum(result)/len(result)) #take average
        value = int(str)
return value`

when I print the value I am always getting 1994. Can someone explain why to me?

Hi there! The 1994 result you’re getting is from the last item in stripped_test_data, averaging out 1990 and 1999. The function is looping through your list and doing its job, but since the value from each iteration isn’t being appended to anything (like a new list), you’re only getting back the value from the last iteration.

1 Like

Hey April, thank you for helping me out. I get it now!