Learn Python 3: Strings - Project - Thread Shed - Step 11 - Index out of range error

Hi I am hoping somone can shine some light on where I am going wrong:

For Thread Shed step 11 the step involves:

iterate through transactions_clean and for each transaction:

  1. Append the customers name to customers .
  2. Append the amount of the sale to sales .
  3. Append the threads sold to thread_sold .

My code for this step is:

customer = []
sales = []
thread_sold = []

for transaction in transactions_clean:
  customer.append(transaction[0])
  sales.append(transaction[1])
  thread_sold.append(transaction[2])

and I keep returning the following error:

Traceback (most recent call last):
  File "script.py", line 132, in <module>
    customer.append(transaction[1])
IndexError: list index out of range

I think the issue exits in the previous step where I created transactions_clean but cant get my head around what I have done wrong. My code for the previous step is:

transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for i in transaction:
   transaction_clean.append(i.replace("\n","").strip(" ")) 
  transactions_clean.append(transaction_clean)
#print(transactions_clean)

If anyone can help “Shed” some light on where I’ve gone wrong that would be most appreciated?

I eventually figured it out! The issue in the end and the issue was actually earlier in my code than I expected. In the first part of the exercise I was using a comma to split and replace daily_sales and that is what was causing the error later on

Incorrect code:

daily_sales_replaced = daily_sales.replace(";,;",",")

daily_transactions = daily_sales_replaced.split(",")

#print(daily_transactions)

daily_transactions_split = []

for transaction in daily_transactions:

daily_transactions_split.append(transaction.split(","))

#print(daily_transactions_split)

Correct Code:

daily_sales_replaced = daily_sales.replace(";,;",":")

daily_transactions = daily_sales_replaced.split(",")

#print(daily_transactions)

daily_transactions_split = []

for transaction in daily_transactions:

daily_transactions_split.append(transaction.split(":"))

#print(daily_transactions_split)
1 Like

hey! Im glad you figured it out. I a running into the same issues. I have double and triple checked my code, even rewrote it a few times. Everything looks as it should but I am still getting an index error when I go to print the customers, sales, and thread_sold. Im going to keep looking over it but if you could offer any advice, I would extremely appreciate it. Thanks!!

If you share your code I will take a look and see if I can help