Hey guys I dont know why but I keep on getting a index error even though my lists are in all range. I was wondering if anyone could help me at on where im going wrong, would really appreciate it. Thanks!
customers = []
sales = []
thread_sold = []
for data_point in transactions_clean:
customers.append(data_point[0])
sales.append(data_point[1])
thread_sold.append(data_point[2])
print(customers)
print(sales)
print(thread_sold)
replace the word, “data_point” with “item” and see what happens.
will do thanks for the reply!
its still out of range according to the terminal, im really confused as to why its out of range even though its exactly how the solution is
But, it’s not how the solution looks. Sometimes it’s helpful to break up the code blocks into smaller pieces—rather than have a wall of code which is hard to sift through–so you can determine where exactly the error is. (which is what I just did in a code editor).
Okay, this line:
daily_sales_replaced = daily_sales.replace(";,;", "|")
where is the pipe “|” coming from?
That symbol shouldn’t be in the code. What’s currently there and what should be replaced?
Should be:
daily_sales_replaced = daily_sales.replace(';,;', '+')
This line:
daily_transactions_split.append(transaction.split(";,;"))
Should be:
daily_transactions_split.append(transaction.split('+'))
>>[['Edith Mcbride ', '$1.21 ', ' white ', '\n09/15/17 '], ['Herbert Tran ', ' $7.29', '\nwhite&blue', ' 09/15/17 '], ['Pa...e
The way you have this section of code:
transactions_clean = []
for transaction in daily_transactions_split:
transaction_clean = []
for data_point in transaction:
transaction_clean.append(data_point.replace(" \n", "").strip(" "))
transactions_clean.append(transaction_clean)
print(transactions_clean)
#produces this, which is incorrect...b/c it's not a list of lists.
>>>>[['Edith Mcbride |$1.21 | white |09/15/17'], ['Herbert Tran | $7.29|white&
Which is why the last bit of code throws that error, "list index out of range:
customers = []
sales = []
thread_sold = []
for data_point in transactions_clean:
customers.append(data_point[0])
sales.append(data_point[1])
thread_sold.append(data_point[2])
print(customers)
print(sales)
print(thread_sold)
There is no index 1 or 2, b/c there’s only one item at index 0 in your output. (which is what 'list index out of range" error means, you’re attempting to access an element at an index that doesn’t exist).
It’s supposed to produce a list of lists, like this:
transactions_clean = []
for transaction in daily_transactions_split:
transaction_clean = []
for data_point in transaction:
transaction_clean.append(data_point.replace('\n', '').strip(' '))
transactions_clean.append(transaction_clean)
print(transactions_clean)
>>[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran', '$7.29', 'white&blue', '09/15/17'], ['Paul Clarke', '$12.52', 'white&blue', '09/15/17'], [...
oh I see. thank you so much!