Thread Shed - color count


#1

code
thread shed exercise

Hi,

When I try to execute step 19 I got a different answer that the final solution. The task is to define a color_count function to count how many threads there are from a certain color.
My thread_sold_split list looks like [‘white’, [‘green’, ‘white’, ‘blue’], etc… ]. If I use my function it only counts the threads mentioned outside the sublists in thread_sold_split. I tried to use a for loop in the sublist but I don’t get the function to count these colours in it. What do I miss? Please do correct my language.

Thank you.
kR
Joke


#2

Hi @jokeblockx0115999330 ,

Please supply a link to the Thred Shed project so that users can find it easily.


#3

Thanks for editing in the link.

Does the if block in this code do what you intended? …

thread_sold_split = []

for thread in thread_sold: 
  if '&' in thread:
    single = thread.split('&')
    thread_sold_split.append(single)
  else: 
    thread_sold_split.append(thread)

What type of object is single?


#4

your welcome.
thank you for your reply.
with the If block I try to separate ‘white’ from ‘white&blue’ although I need to append them both to the empty list thread_sold_split. So in order to make these two equal I need to split the latter “white&blue”. I think that single is a variable. by adding the split method to ‘single’ I create an additional list in the list thread_sold.


#5

You can make it easier by using split on every item in thread_sold, regardless of whether it contains '&'. If it does not, then no harm is done; in that case, you will have a list with only one item. Then you can use an inner loop to handle the individual colors for every sale. Try something like this, using variable names of your own choosing …

thread_sold_split = []
for item in thread_sold:
  for thread in item.split("&"):
    thread_sold_split.append(thread)

#6

thank you. it makes very much sense.


#7

Glad it worked. :smile:

Alternatively, you could create the thread_sold_split object in a single line, using a list comprehension, though that is not required by the instructions.


#8

i am glad for this website so that i can actually learn true coding