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

Converting list of strings to int

Screen Link:
https://app.dataquest.io/c/62/m/351/cleaning-and-preparing-data-in-python/8/parsing-numbers-from-complex-strings-part-two

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(string):
    if "-" in string:
        dates = string.split("-")
        for year in dates:
            year = int(year)
        
        #dates = [int(year) for year in dates]

        update = round(sum(dates)/2)
    else:
        update = int(string)
    return update

processed_test_data = []
for date in stripped_test_data:
    update = process_date(date)
    processed_test_data.append(update)
    
for row in moma:
    date = row[6]
    date = strip_characters(date)
    date = process_date(date)
    row[6] = date

What I expected to happen:
I expected the

        for year in dates:
            year = int(year)

portion of my code to work as how the commented out line below it does:

dates = [int(year) for year in dates]

What actually happened:
To my understanding, it seems that the for loop is not converting all elements to int.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-17c6921909ff> in <module>
     88 processed_test_data = []
     89 for date in stripped_test_data:
---> 90     update = process_date(date)
     91     processed_test_data.append(update)
     92 

<ipython-input-1-17c6921909ff> in process_date(string)
     81         #dates = [int(year) for year in dates]
     82 
---> 83         update = round(sum(dates)/2)
     84     else:
     85         update = int(string)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

I ran your code and it didn’t throw any errors. Are you sure you shared all the code?

I recommend refreshing the page and running the code again.

I have updated my post to reflect the version that throws me the error. I had it commented out before so it would stand out, sorry!

Yes, that’s what’s happening.

The year in the above loop gets updated but the list item is separate from that variable year, to put it simply.

You can -

  • Use list comprehensions (like you did), or
  • Create a new list and append the modified values to that new list, or
  • Iterate over the indices using either range() or enumerate() and then update the values using something like dates[idx]
1 Like