Thread Shed Exercise 18

link: []

When trying different things to solve Ex. 18, I stumbled upon sth I cannot yet understand.
To begin with, this is my output for print(thread_sold):

['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', ...]

The task is:" iterate through thread_sold. For single colors, append that color to thread_sold_split . If it is multiple colors, first split the string around the & character and then add each color indivudally to thread_sold_split .

My code:

for i in thread_sold:
  if "&" not in i:

Now the tricky part. For testing, I try the line:


which gives me this:

['white', 'white', 'white', 'white', 'white', 'purple', 'purple', 'purple', 'blue', 'blue', 'purple', 'white', 'white', 'white', 'blue', 'green' ...]

Which is correct.
But when attempting to add list comprehension like this:

    thread_sold_split.append(i.split("&")[t] for t in range(len(i)))

I get this as a result:

['white', <generator object <genexpr> at 0x7f0db0a8d570>, <generator object <genexpr> at 0x7f0db0a8d5c8>, 'white', <generator object <genexpr> at ...]

Which isn’t wrong, I think, it just gives the location of the string element instead of its label. I don’t know how to change that though… Any help or should I try a different approach?

Hi there.

No, it is wrong… and <generator object <genexpr> at ...> isn’t a string. Points for the attempt, though, I can see what you were going for there. :+1:

You already have this:
so you clearly know that split() returns a list, but you perhaps don’t know precisely how many things are in that list.

Can you think of a way of adapting your code so that you split the combined string - white&red for instance - and then add each individual colour which comes back to your thread_sold_split list? :slight_smile:

1 Like

You’ve already figured out how to add the first item from the list you get back from split(). Using a programming structure you’re already using in your program, how can you repeat that for every item in the list that comes back from split()?

You can evaluate a generator by iterating through it (for example with a loop or by converting it to list)
also, if you’re going to split on something then it’s irrelevant whether it’s there or not. just split it

1 Like

I assume I can’t find a concise solution here? I was trying for a one liner :sweat_smile:

1 Like

Get it right before you get it pretty. I suggest evaluating your generators and looking at what you have after that.
Also make sure you’re super sure about what actions you want to be carrying out since that is your reference and without that you probably won’t be doing the right things.

Also, rather than looking at the end result which is a large amount of output and hard to check, look at before and after splitting for just a single transaction so that you can check those individually.

Presumably you’d start with something like:


and want to end up with:

['blah', 'bleh']

(or use a smaller input with a just a few transactions)


I’m not 100% sure what “evaluating generators” means, but I got this:

    for t in thread_split:

I still wonder, if there is a way to make it shorter…

the second line overwrites whatever the first one does
And you can take a look at what operations a list supports

1 Like

edited, thanks. But a simple yes or no would have sufficed, too :wink:

If you use something more suitable than append then that would be one line
You have multiple values all of which should be added to the list. list has a method for doing that, eliminating the repeated (loop) use of append


the thing is why you adding t to thread_split again even when you have another list (empty one)