Learn-python-3/projects/thread-shed - step 8

https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

# Start coding below!

daily_sales_replaced = daily_sales.replace(';,;', '_')

daily_transactions = daily_sales_replaced.split(',')

#print(daily_transactions)

daily_transactions_split = []

for trans in daily_transactions:

  daily_transactions_split.append(trans.split('_'))

#print(daily_transactions_split)

transactions_clean = []

for transactions in daily_transactions_split:

  transactions_clean = []

  for data_points in transactions:

    transactions_clean.append(data_points.replace('\n', '').strip(' '))

    transactions_clean.append(transactions_clean) 

#print(transactions_clean)

why i’m not getting the complete list here?
please help and please update your youtube project walkthrough its not helped me!

Hi! Welcome to the forums!

Please format your code by pressing </> before pasting. It will give everyone a better chance to help you out. Indentation matters a lot in python, and there can be all sorts of things making this code not work that have to do with syntax like that.

1 Like

Ok, this bit of your code might have trouble working, and it has to do with indentation and how loops work:

transactions_clean = []

# A
for transactions in daily_transactions_split:
  transactions_clean = []

#B
  for data_points in transactions:
    transactions_clean.append(data_points.replace('\n', '').strip(' '))
    transactions_clean.append(transactions_clean) 

Right now as it stands it reads like this:

  • set the value of transactions_clean to be []
  • for every transaction in daily_transactions_split, do the following
  1. set the value of transactions_clean to be [] (again)
  2. run the code at #B (which only affects transactions_clean)

That means if you have a daily_transactions_split with 5 items, the code will run 5 times, and every time it will set the value transactions_clean to be [] (essentially resetting whatever changes you made to it every time). The very last loop, it will retain the value of the code at #B.

If I were to guess I would say if you were following the video you missed an indentation patter or two. If I personally want to follow a tutorial video, I try to make sure I digest every block of code a little bit before going on. Even if it means stopping, playing around with it, etc. It’s slower but the point of the exercise is not completion, as much as it is gaining familiarity with the tools.

1 Like

now i reached to

transactions_clean = []
for transactions in daily_transactions_split:
  for data_points in transactions:
    transactions_clean.append(data_points.replace('\n', '').strip(' '))
    transactions_clean.append(transactions_clean)
print(transactions_clean)

please help me to clean more!

When dealing with for-loops, before I start nesting (putting other loops inside of loops), I just do a very honest take and print out the items in it just to double check what’s inside: (it’s important to gain control over this, the answer is not the point, the point is the control)

list_1 = ["The, "weather", "is", "nice"]
list_2 = [1, 2, 3]

for item in list_1:
   print(item)
# The
# weather
# is 
# nice

for lst1_item in list_1:
    print(lst1_item)

    for lst2_item in list2:
        print(lst2_item)

# The
# 1
# 2
# 3
# weather
# 1
# 2
# 3
# is 
# 1
# 2
# 3
# nice
# 1
# 2
# 3
1 Like

i tried my best but now can anyone write the correct code, Please!
Thank YOU!

As this is a learning environment most folks would and generally should avoid just giving answers. The point of the lessons isn’t just to tick them off and say you’ve done them. You’re here to learn right? Getting stuck on a point is natural and you should take the time to work through it properly as you’ll have a better chance of remembering it in the future. Learning to analyse and debug code is incredibly important too; if you take anything away from this lesson then that’s what you should remember. You’ll never stop using that kind of skill but it’s one you need to practice at.

As per previous guidance try breaking it down so you know what happens at every step of your loop. Using print statements and such is a good way to look into this. It’d also be worth having a very close read of previous posts; the actual issue has already been mentioned. Follow through the given information carefully and take your time to solve it yourself, it’s worth it.

3 Likes