Thread Shed, nested loop

Hi everyone,

I am struggling on the Thread Shed project, Task 8.

Now, Iterate through daily_transactions_split and for each transaction iterate through the different data points and strip off any whitespace.

The code starts out with a giant string of which I’ve posted a snippet

daily_sales = \
"""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 ,Lucille Caldwell   
;,;   $5.13   ;,; white   ;,; 09/15/17,
Eduardo George   ;,;$20.39;,; white&yellow 
;,;09/15/17   ,   Danny Mclaughlin;,;$30.82;,;   
purple ;,;09/15/17 ,Stacy Vargas;,; $1.85   ;,; 
purple&yellow ;,;09/15/17"""

Here is my code to “clean it up,” but I can’t get the last part to work, the part where I need to iterate through and remove white space.

Here is my code. This part works.

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

daily_transactions = daily_sales_replaced.split(",")

daily_transactions_split = []

for each in daily_transactions:
  split_by_dash = each.split("-")
  daily_transactions_split.append(split_by_dash)

This part doesn’t, and I can’t figure out why

transactions_clean = []

for each_transaction in daily_transactions_split:
    for item in each_transaction:
      item.strip()
transactions_clean.append(each_transaction)

I watched the video and it confused me even more.
Any help would be appreciated!

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

Maybe I pasted something wrong, because it is not throwing an error, it just doesn’t strip out the white space. Here is the full thing

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

daily_transactions = daily_sales_replaced.split(",")

daily_transactions_split = []

for each in daily_transactions:
  split_by_dash = each.split("-")
  daily_transactions_split.append(split_by_dash)

transactions_clean = []

for each_transaction in daily_transactions_split:
    for item in each_transaction:
      item.strip()
transactions_clean.append(each_transaction)

I stand corrected. It does not throw an error… it seems to leave the last iteration in memory. The problem remains the same. Because of the indentation, it’s only going to do that thing once, not for each transaction. I guess it’s not a situation I run into a lot because it’s rather rare to try to use the last iterated value in a special way outside of the loop (is it bad practice? I can’t say for sure. My intuition thinks it is).

test = [1,2,3]
for i in test:
  print(i)
#1
#2
#3
test.append(i)
print(test)
# [1, 2, 3, 3]

Meaning you still probably want transactions_clean.append(each_transaction) to be indented to fit the outer loop.

test = [1,2,3]
new_list = []
for i in test:
  new_list.append(i)
print(new_list)
# [1, 2, 3]

For comparison @adamfrisbee :

test = [1,2,3]
new_list = []
for i in test:
  pass
new_list.append(i)
print(new_list)
# [3]

@adamfrisbee There’s also the added nuance that the strip() method only returns a value (it doesn’t change it). So you have to immediately store that return value somewhere.

test = " hello "
test.strip()
print(test)
# " hello "
print(test.strip())
# "hello"
print(test)
# " hello "
1 Like

If I understand you correctly, I need to have two empty lists

transactions_clean = []
second_list = []

I’m afraid I need a bit more guidance.

for each_transaction in daily_transactions_split:
    for item in each_transaction:
      item.strip()
  transactions_clean.append(each_transaction)
1 Like

This is good, re-look over the explanation for strip() (I linked to documentation below). It’s very close. If you put a couple of print statements it may clarify to you what your code is doing. (Your transactions_clean is already your second list so you’re good on that front).

for each_transaction in daily_transactions_split:
    for item in each_transaction:
      item.strip() # this is the one to think about
  transactions_clean.append(each_transaction)

https://docs.python.org/3/library/stdtypes.html

1 Like

Thanks for your help. I think I got it, or at least very very close!

transactions_clean = []
each_transaction_list = []

for each_transaction in daily_transactions_split:
  for item in each_transaction:
    each_transaction_list.append(item.split())
  transactions_clean.append(each_transaction_list)
2 Likes