Hey guys, im currently at the exercise “Thread Shed”. At Step 17 I get confused about the proper solution, which is given within the Project Walkthrough.
Im really getting confused, that kind of confusion that makes you feel like “I don’t understand anymore how the world works”
https://www.codecademy.com/courses/learn-python-3/projects/thread-shed
The solution looks like this:
thread_sold_split = []
for sale in thread_sold:
for color in sale.split("&"):
thread_sold_split.append(color)
What happens in the line ‘for color in sale.split(“&”)’?
I mean I think it’s supposed to iterate through a list in a list (a loop nested in another). That means in this case, because there is just one List and in that list there are several strings, I will iterate through the letters of each string in that list.
But if I print the color before the last line, which appends the color to the new list, I don’t see the single letters printed but the whole string (the color for example: white, green, blue etc.) and not (w, h, i, t, e …).
But if I do write a code like
thread_sold_split = []
for sale in thread_sold:
for color in sale:
sale.split("&")
thread_sold_split.append(color)
it prints me every single letter, which im supposed to receive but still with the “&” - sign. So the sale.splitt(“&”) didn’t do anything.
But changing my code to:
hread_sold_split = []
for sale in thread_sold:
for color in sale:
splitted = sale.split("&")
thread_sold_split.append(splitted)
it obviously filters out the “&”-sign and returns the whole word into a new list. But now I have a list in a list, which im not supposed to have. I mean, I know why, because I saved the changes of ‘sale.split(“&”)’ in an empty list called ‘splitted’.
So what’s going on in the solutions line ‘for color in sale.split(“&”)’ ? Why do I get whole words if I print (color) and not the single letters?
for sale in thread_sold:
for color in sale.split("&"):
print(color)
thread_sold_split.append(color)
Looking forward for some answers that gets me out of that confusion.
Kind regards