Thread Shed Challenge - Step 18 Question

I finished the problem, but I struggled on step 18 and ultimately had to find the solution online. Can someone give a more detailed explanation of what the code snippet here is doing for step 18? Since the original question 18 wanted us to see if the thread was a single color or multiple-colors, I was under the impression that I would have to use an IF statement of some sort. But obviously the solution doesn’t feature that, so I was just wanting some more explanation behind the solution. Thanks!!!

thread_sold_split = []

for x in thread_sold:

  for color in x.split('&'):

    thread_sold_split.append(color)

#print(thread_sold_split)

It’s useful to consider what split does:

>>> string = "hello&world"
>>> string.split("&")
['hello', 'world']

I only remember this exercise vaguely, but with a bit of deduction

  • x in thread_sold is iterating through strings that contain “&”, you should verify this with a print statement.

  • splitting these strings yields a list of strings, and you’re iterating through every string and depositing in your container list thread_sold_split (again, you should insert a print statement to highlight it).

2 Likes

Gotcha, thanks for helping clear this up!