Thread Shed

https://www.codecademy.com/courses/learn-python-3/projects/thread-shed
Some where between steps 6 and 8, my list stopped being nested and became one long list of transactions. When I reviewed the video walk through for the project they managed to maintain nested lists. It also appears that “/n” escape was stripped out though I never programmed for that. Can someone explain to me what happened to the “/n”'s and why the lists were no longer nested?

# Start coding below!

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:
  for thing in transaction:
    transactions_clean.append(thing.strip())

#print(transactions_clean)

customers = []
sales = []
thread_sold = []

for i in range(len(transactions_clean)):
  if i % 4 == 0:
    customers.append(transactions_clean[i])
  elif i % 4 == 1:
    sales.append(transactions_clean[i])
  elif i % 4 == 2:
    thread_sold.append(transactions_clean[i])```

This actually happened to me too, though I couldn’t figure out why. Eventually, I figured out a solution using indexing and list comprehension.

transactions_clean = [transactions[x:x+4] for x in range(0, len(transactions),4)]
range(0, len(transactions), 4) 
#gives you a list of numbers in len(transactions) but skipping by 4
#When you print as a list, it's something like this:
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268, 272, 276, 280, 284, 288, 292, 296, 300, 304, 308, 312, 316, 320, 324, 328, 332, 336, 340, 344, 348, 352, 356, 360, 364, 368, 372, 376, 380, 384, 388, 392, 396]
transactions[x:x+4] for x in...
#First cycle, x is 0 so first mini list will be transactions[0:4] and we put +4 because the end index is not included. 
#Snippet
['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']
#Mini list will be equal to ['Edith Mcbride', '$1.21', 'white', '09/15/17'] 
#Notice how these indexes are 0, 1, 2, 3, and we indexed transactions for [0:4]? 
#And it just repeats

I did this using a list comprehension, but it’s easily repeatable using a for loop.

new_lst = []
for x in range(len(transaction)):
  new_lst.append(transaction[x:x+4])

As for the \n’s they disappeared for me too, weird.
This has been occurring a lot for me. My theory is that whenever a string with \n’s is split into a list, you automatically lose the \n’s.
Happened to me with the Censor Dispenser Project.

1 Like