Why is it that my for loop is not iterating through the whole list?

Hello! I have been coming to this forum often to check answers and problems when i get stuck and just to learn other techniques others may have. Up until now i have not posted anything, but i am stuck on the Thread Shed Challenge so this is my first question in the forums. I wanna thank you all for your help already. So, when i run my code all i get is

[[‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’]]

And This is my 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)

transactions_clean =

for transaction in daily_transactions_split:
transaction_clean =

for data_point in transaction_clean:
transaction_clean.append(data_point.replace(“\n”, “”).strip(" "))

transactions_clean.append(transaction_clean)

print(transactions_clean)

customers =
sales =
thread_sold =

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

#print(customers)
#print(sales)
#print(thread_sold)

Why is it that my for loop is not iterating through the whole list and instead it is only giving me one customer, price, and thread? I have been stuck on this problem for 2 days trying to work myself out of it but now it’s time to suck it up and ask for help!!! Can anyone please explain this to me??

We cannot tell the code structure here, but one suspects both of these appends are inside the loop. The latter should NOT be in the loop, but after it.

I followed the guy on the video solution for the problem after i got stuck. So it is the video solution provided for the problem.

for data_point in transaction_clean:
    transaction_clean.append(data_point.replace("\n", “”).strip(" "))

transactions_clean.append(transaction_clean)

Does your code look like that?

1 Like
 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)

No. This is how i coded it after what the video solution told me to do but something is wrong somewhere because i still haven’t got past #12 on this problem.

1 Like

As suspected, both appends are inside the inner loop;

1 Like

Well, i get a little confused (okay more than a little) with for loops. Can you explain that to me a little more and better? Excuse me, im a newbie to coding and im starting from ground zero. I’ve done other for loops and haven’t had as difficult a time as i am on this one.

Consider the scope in which a given variable is to exist. One inside the loop, one outside.

Okay, from what you are saying, does that mean that i need to move the line

transactions_clean.append(transaction_clean)

outside of the loop?

There are two loops, one nested in the other, are there not? One of the arrays gets appended in the nested loop, the other in the outer loop. You have the right idea, though. Just make sure they’re not both being appended inside the same loop body.

loop
    loop
        tran_clean.append(transaction)
    end
    trans_clean.append(tran_clean)
end

That is pseudo code, not Python.

3 Likes

2 posts were split to a new topic: Can you explain me the “-1” after che t i.find(“&”)?

THANK YOU! I have a better understanding after this easy explanation! :star_struck:

1 Like

71 posts were split to a new topic: Getting 27 for the “white” count instead of 28