Python Thread Shed project

This question is regarding:
https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

I thought I had everything going ok, then I noticed that I somehow managed to take a list starting with a length of 100 and turn in into 400, and then created 3 separate list of 400 each. I have no idea where I went wrong.
I’m only posting my code vs posting the strings from the exercise because its a very long list, but the starting strings can be found in the project link or I can post the whole thing if its requested.
Thanks for helping me figure out how my list got out of hand…

daily_sales_replaced = daily_sales.replace(";,;", “:”)
daily_transactions = daily_sales_replaced.split(",")
print(len(daily_transactions))

daily_transactions_split =
for x in daily_transactions:
y = x.split(":")
daily_transactions_split.append(y)
print(len(daily_transactions_split))

transactions_clean =
for x in daily_transactions_split:
for y in x:
z = y.strip()
transactions_clean.append(y)
print(len(transactions_clean))

customers =
sales =
thread_sold =

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

print(len(customers))
print(len(sales))
print(len(thread_sold))

Where does z come into play?

Where does x come into play?

2 Likes

@mtf I thought I had to give an additional label (z) in order for the code to have somewhere to strip.
I’ve since gone through and removed “z =” and left the y.strip(), but it still results in the same output.

As for the x, don’t I have to run that as a loop to get the desired result? That being having every 4th item starting with index zero in the list added to a new list? I don’t know how else to do that as a loop without assigning it some temporary name, so I put x.

This is my first time trying to learn coding, so I’m sure I’m missing something.

If the output is still four times the expected output, then look for appends within nested loops. We’re not able to read the blocking of your code since it is not formatted. Have you read the new user literature?

@mtf

@mtf Is that what you mean by needing my code formatted?
I’ve read the new user literature, but I was trying to make it so that it was less for anyone helping me to read through.
I’m hoping I’m providing everything needed to get assistance, but as I’ve mentioned, I’m new to this.
As a physical therapist assistant and a published thriller author, I have zero experience in coding and am still learning the lingo. So sometimes I need things spelled out for me.
I am good at problem solving, and believe coding will be a good fit for me once I get a better understanding though, so I’m hoping the forums can help me out with that.

A step by step of what I THINK my code does (vs what it’s actually doing):

  1. replace every instance of “;,;” with “:”
  2. split a long string into a long list where the split occurs at “,”
  3. split that list into several list within a list where the split occurs at “:”
  4. take that list and remove the white space, creating a new list without random white space
  5. create three new list (customers, sales, thread_sold)
  6. separate the relevant information to the three new list

My problems seem to occur in steps 3 and 4, but I can’t for the life of me figure out what the problem is.

transactions_clean = []
for x in daily_transactions_split:
  for y in x:
    y.strip()
    transactions_clean.append(y)
print(len(transactions_clean))  

The above is taking 100 rows and appending everything to the one list, making it 400 rows.

What happens if you create an empty list on each iteration of outer loop, then append the four items in each row to that list, then outside of the inner loop append the new list to the transactions_clean list?

I just tried that. I was literally watching the video walk thru as your reply hit. I hate using the video walk thru because I feel like they show the answer without explaining why. But I’m desperate so I did it anyhow.
Now the problem is, not only am I still seeing a jump of 100 to 400 at the transactions_clean step, but it made the following step (which originally was working fine) now just take the same data and repeat it 400 times into the customer/sales/thread_sold lists instead of putting the appropriate data in there.
Again, I literally copied what the video said to do, only changing list names to decrease keystrokes.
How am I getting such drastically different results than the person in the video?

  for data in transaction:
    tran_list.append(data.replace("\n", "").strip(" "))
    transactions_clean.append(tran_list)

The second append should be in the outer loop, not the inner one. The above is simply repeating what your previous code did.

1 Like

Ok, so it was an indentation issue. Once I fixed the indentation issue the numbers all worked out. Thanks for helping me see that @mtf

Here’s a corrected version of Thread Shed up to step 12 for anyone who might need it:

1 Like

You’re welcome. Glad you got it working.

Well @mtf I at least got that one part working :rofl: I still have to figure out why the next part isn’t working correctly.

The first picture is my code and the output. The second code, that looks identical to me, is the video answer with a completely different output.
The mysteries continue.

Lines 132 - 134 should be extracting from x, not transactions_clean.

customers.append(x[0])

This is also an opportunity to convert the dollar values to float.

sales.append(float(x[1].strip('$')))
1 Like

I can’t believe I missed that! That completely makes sense!
Thanks @mtf
Thanks for the tip on converting the dollar values to floats too. I’m definitely going to implement that.

1 Like

You do have a strip method call in the transactions clean loop. We can strip the $ in that action,

 data_point.strip('$ /n')

all in one go.

So now this line will look like,

sales.append(float(x[1]))
1 Like

I didn’t realize you could strip multiple disconnected things at once. I thought you would have to run strip multiple times for that.
Is it like using replace where the items have to be separated by a comma? Or do you just leave a space between the things you want to strip?
That’s a huge tip. That has the potential to save me a lot of time. Thanks!

Replace is explicit, and as far as I know we cannot replace a list, only a single expression at one time.

str.strip() takes a single argument but it can be made up of multiple characters to strip. In the above we strip $, space, and newline in no particular order. Only the characters that are found are stripped.

1 Like

That’s awesome. I’m really glad you shared that. I don’t think they mention in the lesson that you can use it like that. Thanks again.

1 Like