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

Why are numbers in brackets

Hello,
I had a question about “Parsing Numbers from Strings Part 2” https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/8/parsing-numbers-from-complex-strings-part-two

stripped_test_data = ['1912', '1929', '1913-1923',
                      '1951', '1994', '1934',
                      '1915', '1995', '1912',
                      '1988', '2002', '1957-1959',
                      '1955', '1970', '1990-1999']
def process_date(date):
    if "-" in date:
        split_date = date.split("-")
        date_one = split_date[0]
        date_two = split_date[1]       
        date = (int(date_one) + int(date_two)) / 2
        date = round(date)
    else:
        date = int(date)
    return date

I don’t understand why 0 and 1 are in brackets in the “def process_date(date)” (i.e., date_one = split_date[0]).

1 Like

Hi @mtl1212

When we apply the .split() method in a string it will break it at the position where the character specified is and creates a list, so we use numbers to select the position of each element in the list. For example:

'1990-1999'.split(-) = [1990, 1999] = split_date
split_date[0] = 1990
split_date[1] = 1999

Hope i made myself clear

Good luck!

3 Likes

Hi,
Because it is a list are those numbers in the parentheses index numbers?

Yes. It creates a new list as specified in the documentation. split_date[0] and split_date[1] retrieves the start and end date respectively.

1 Like