Thread Shed 17/18

Hi guys I’m having trouble with steps 17/18 in the ‘Thread Shed’ activity. I’m supposed to be getting a string of individual colors but my output is giving me lists instead. Here is my code:

thread_sold_split = []

for i in thread_sold:
  temp = []
  if i.find('&'):
    temp.append(i.split('&'))
    for j in temp:
      thread_sold_split.append(j)
  else:
    thread_sold_split.append(i)
print(thread_sold_split)

and here is my output:
[['white'], ['white', 'blue'], ['white', 'blue'], ['white'], ['white', 'yellow'], ['purple'], ['purple', 'yellow'], ['purple', 'yellow'], ['blue'], ['blue'], ['purple', 'blue'], ['white'], ['white', 'red'], ['white', 'blue', 'red'], ['blue'], ['green', 'blue'], ['green', 'blue', 'red'], ['green', 'blue', 'red'], ['black'], ['black', 'yellow'], ['white', 'black', 'yellow'], ['white', 'black', 'yellow'], ['green'], ['green', 'yellow'], ['green', 'yellow', 'blue'], ['green', 'yellow', 'purple', 'blue'], ['black'], ['black', 'blue'], ['black', 'blue'], ['black'], ['black', 'purple'], ['black', 'purple'], ['yellow'], ['yellow', 'red'], ['yellow'], ['yellow', 'blue'], ['yellow', 'blue', 'red'], ['black'], ['black', 'red'], ['black', 'red'], ['white', 'black', 'red'], ['yellow'], ['yellow', 'black'], ['green', 'yellow', 'black'], ['yellow'], ['white', 'yellow'], ['white', 'yellow', 'black'], ['yellow'], ['yellow'], ['white'], ['white', 'black'], ['white', 'black'], ['white', 'black', 'red'], ['purple'], ['purple', 'yellow'], ['purple', 'yellow'], ['green', 'purple', 'yellow'], ['red'], ['yellow', 'red'], ['green', 'yellow', 'red'], ['red'], ['green', 'red'], ['green', 'white', 'red'], ['white'], ['white', 'red'], ['purple'], ['purple'], ['green'], ['green'], ['green'], ['red'], ['white', 'red'], ['white', 'purple', 'red'], ['red'], ['black', 'red'], ['black', 'red'], ['black', 'red'], ['green'], ['green', 'yellow'], ['green', 'yellow', 'blue'], ['purple'], ['purple', 'black'], ['yellow'], ['yellow'], ['yellow', 'blue'], ['green'], ['green'], ['white'], ['white', 'blue'], ['white', 'black', 'blue'], ['green'], ['green', 'yellow'], ['green', 'yellow', 'black'], ['green'], ['green'], ['green', 'purple'], ['green'], ['green', 'white'], ['green', 'white', 'blue'], ['green', 'white', 'blue']]

Not sure how to fix this. Any help is appreciated. Thanks.

Edit: Also tried this with the same result:

thread_sold_split = []

for i in thread_sold:
  if i.find('&'):
    thread_sold_split.append(i.split('&'))
  else:
    thread_sold_split.append(i)
print(thread_sold_split)

If it is a list with lists of strings, you have multiple options, but I can think of two off the top of my head.

#1
new_lst = []
for lst in thread_sold_split:
  for items in lst:
    new_lst.append(items)
#2
thread_sold_split_new = ",".join(thread_sold_split)

Off the top of my head, might be some errors in there.

Would this be to process my output into the correct form? What I am curious about is how to make my code so that it doesn’t require processing a second time. I know I could loop back over the list of lists, but it seems inefficient. I don’t understand WHY my output is incorrect to begin with.

Is this the sort of outcome you are looking for?

Color: red, Count: 24
Color: purple, Count: 17
Color: white, Count: 28
Color: black, Count: 26
Color: yellow, Count: 34
Color: blue, Count: 22
Color: green, Count: 30

No that is one step further than I am currently. Right now I need something like this:

['white', 'blue', 'red', 'yellow', 'blue', 'yellow'] etc.

Ideally that should be the output from one function. The input data looks like this:

['white', 'white&blue', 'white&blue', 'white', 'white&yellow', 'purple', 'purple&yellow', 'purple&yellow', 'blue', 'blue', 'purple&blue', 'white', 'white&red', 'white&blue&red', 'blue', 'green&blue', 'green&blue&red', 'green&blue&red', 'black', 'black&yellow', 'white&black&yellow', 'white&black&yellow', 'green', 'green&yellow', 'green&yellow&blue', 'green&yellow&purple&blue', 'black', 'black&blue', 'black&blue', 'black', 'black&purple', 'black&purple', 'yellow', 'yellow&red', 'yellow', 'yellow&blue', 'yellow&blue&red', 'black', 'black&red', 'black&red', 'white&black&red', 'yellow', 'yellow&black', 'green&yellow&black', 'yellow', 'white&yellow', 'white&yellow&black', 'yellow', 'yellow', 'white', 'white&black', 'white&black', 'white&black&red', 'purple', 'purple&yellow', 'purple&yellow', 'green&purple&yellow', 'red', 'yellow&red', 'green&yellow&red', 'red', 'green&red', 'green&white&red', 'white', 'white&red', 'purple', 'purple', 'green', 'green', 'green', 'red', 'white&red', 'white&purple&red', 'red', 'black&red', 'black&red', 'black&red', 'green', 'green&yellow', 'green&yellow&blue', 'purple', 'purple&black', 'yellow', 'yellow', 'yellow&blue', 'green', 'green', 'white', 'white&blue', 'white&black&blue', 'green', 'green&yellow', 'green&yellow&black', 'green', 'green', 'green&purple', 'green', 'green&white', 'green&white&blue', 'green&white&blue']

I realized the solution. The following code works perfectly:

thread_sold_split = []

for i in thread_sold:
  temp = []
  if i.find('&'):
    temp.append(i.split('&'))
    for j in temp:
      for x in j:
        thread_sold_split.append(x)
  else:
    thread_sold_split.append(i[0])
print(thread_sold_split)

I realized I was adding lists to the temp variable, so I had to immediately iterate again in order to get the contents. I don’t fully understand why the colors that didn’t need to be split were being appended as a list, but I saw that they were from my output so I accessed the 0-index and appended those to the new list.

Thank you both for taking the time to help. :slight_smile:

That’s great. Sticking with the problem until a solution is found is the sort of persistence we need in this discipline. Don’t stop there, though. Keep thinking about the problem and see if you can simplify that solution.

Recall that .append() takes a single argument, but .extend() takes a list as an argument. The list may contain only one element, or it may contain several.

thread_sold_split = []
for row in thread_sold:
    thread_sold_split.extend(row.split('&'))

That’s just me thinking out loud, but give that a try and see what happens. The idea, as I see it is to reduce the number of nested loops.

1 Like