Thread Shed - For Loop Indentation

Hi I’m trying to understand the For loop spacing that occurs in my solution and the developer solution.

Specifically in the nested loop in Task 8 where we are trying to clean each items white space but preserve the element format in a new list.

This is the relevant section:


#My solution 
transactions_clean = []

for item in daily_transactions_split:
  transaction_clean = []
  for x in item:
    transaction_clean.append(x.strip())
  transactions_clean.append(transaction_clean)
                                
print(transaction_clean)
print(transactions_clean)

In the developers solution the indentation of the snip and append code is different and so is the outcome.

I am not able to replicate what his spacing appears to be in Spyder without the code throwing an error.

Can someone please help me understand how he is achieving this indentation and what they entail for the output? TIA!

Here is my full code with the data abbreviated:

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 19 12:48:09 2019

@author: peter
"""

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,   Shaun Brock;,; 
$17.98;,;purple&yellow ;,; 09/15/17 """

#------------------------------------------------
# Start coding below!

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

#print(daily_sales_replaced)

daily_transactions = daily_sales_replaced.split(',')

#print(daily_transactions)

daily_transactions_split = []

for line in daily_transactions:
  daily_transactions_split.append(line.split('@'))

#print(daily_transactions_split)


#My solution 
transactions_clean = []

for item in daily_transactions_split:
  transaction_clean = []
  for x in item:
    transaction_clean.append(x.strip())
  transactions_clean.append(transaction_clean)
                                
print(transaction_clean)
print(transactions_clean)

Your indentation is correct. Line 123 in the video appears to be out-dented in a strange position, but that is just an artifact of the editor which had to make an adjustment at the time that the line became too long for the available space. You can see this occurring in the video at approximately 6:25.

Ok. Thanks. Two follow up questions.

  1. Why do I need to have transactions_clean sit outside (less indented) than transactions clean?

I.e.

transaction_clean.append(x.strip())
  transactions_clean.append(transaction_clean)

V.S.

transaction_clean.append(x.strip())
transactions_clean.append(transaction_clean)
  1. Later in the exercise. I am trying to create a new list that splits items if they contain & and thereby two colors. This list contains no sub-lists so I thought the below solution would work but it does not appear to. T
#My CODE 
thread_sold_split = []

for x in thread_sold:
  if '&' in x == True:
    thread_sold_split.append(x.split('&'))
  else: 
      thread_sold_split.append(x)
  
print(thread_sold_split)

The developers solution uses nested for loops. I can’t see why this is necessary given that the thread_sold list has no nested lists.


thread_sold_split = []

for item in thread_sold:
  for color in item.split('&'):
    thread_sold_split.append(color)

print(thread_sold_split)

Here is the full code with the developer’s solution data edited for brevity

TIA

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,   Shaun Brock;,; 
$17.98;,;purple&yellow ;,; 09/15/17 , 
Erick Harper ;,;$17.41;,; blue ;,; 09/15/17, 
Michelle Howell ;,;$28.59;,; blue;,;   09/15/17   , 
Carroll Boyd;,; $14.51;,;   purple&blue   ;,;   
09/15/17   , Teresa Carter   ;,; $19.64 ;,; 
white;,;09/15/17 """

#------------------------------------------------
# Start coding below!

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

#print(daily_sales_replaced)

daily_transactions = daily_sales_replaced.split(',')

#print(daily_transactions)

daily_transactions_split = []

for line in daily_transactions:
  daily_transactions_split.append(line.split('@'))

#print(daily_transactions_split)
#print('KEEP EM SEPERATED')

transactions_clean = []

for item in daily_transactions_split:
  transaction_clean = []
  for x in item:
    transaction_clean.append(x.strip())
  transactions_clean.append(transaction_clean)
                                
#print(transactions_clean)

customers = []

sales = []

thread_sold = []

for sale in transactions_clean:
  customers.append(sale[0])

for sale in transactions_clean: 
  sales.append(sale[1])

for sale in transactions_clean:
  thread_sold.append(sale[2])
  
#print(customers+sales+thread_sold)

total_sales = 0 

for x in sales:  
  total_sales += float(x.strip("$"))

#print(total_sales)

print(thread_sold)

thread_sold_split = []

for item in thread_sold:
  for color in item.split('&'):
    thread_sold_split.append(color)

print(thread_sold_split)