Thread Shed: Step 21 - List Comprehension Possible?

Hello, I am currently doing the Thread-Shed project:
Thread-Shed Project

I am on step 21:

Define a list called colors that stores all of the colored threads that Thread Shed offers:

I know that I could just copy and paste the colors in , but I decided to write code that iterates through the list of colors and - if the color isn’t already in there - adds the color to the list.

colours = []
for colour in thread_sold_split:
  if colour not in colours:
    colours.append(colour)
print(colours)

This code seems to work fine, displaying:

[‘white’, ‘blue’, ‘yellow’, ‘purple’, ‘red’, ‘green’, ‘black’]

But I was wondering if it’s possible to do it as a list comprehension instead. I have tried:

colours = [colour for colour in thread_sold_split if colour not in colours]

Which throws:

Traceback (most recent call last):
File “script.py”, line 159, in
colours = [colour for colour in thread_sold_split if colour not in colours]
File “script.py”, line 159, in
colours = [colour for colour in thread_sold_split if colour not in colours]
NameError: name ‘colours’ is not defined

If I can’t use the list I’m creating in the if statement, I don’t see how to perform this task as a list comprehension. If someone could point out what I’m missing (or any other handy hints), I’d be grateful.

Cheers.

While your idea is cool, in that you are thinking outside the box so to speak, the goal is for a list containing all colors available, not just sold on a particular day. That aside, there’s a few ways to do what you are attempting. You could add only colors that aren’t included in a list slice of the list your getting your data from.
For example:

blardy = [1, 2, 3, 4, 5, 2, 1, 5, 6, 7, 8, 1]
blah = [n for i, n in enumerate(blardy) if n not in blardy[:i]]
print(blah) #[1, 2, 3, 4, 5, 6, 7, 8]

Or, you could use a set comprehension, and convert it to a list: (Topic to search and study if you are unfamiliar)

blardy = [1, 2, 3, 4, 5, 2, 1, 5, 6, 7, 8, 1]
blah = list({n for n in blardy})
print(blah) #[1, 2, 3, 4, 5, 6, 7, 8]
2 Likes

Thanks for the answer. I love the look for set comprehension, cheers for introducing me to it.

Have a good one

1 Like

I left the constructor out of my initial response since you asked about comprehensions. To eliminate duplicate values from a simple one dimensional list, you could use the set constructor, followed by the list constructor:

blardy = [1, 2, 3, 4, 5, 2, 1, 5, 6, 7, 8, 1]
blah = list(set(blardy))
print(blah) #[1, 2, 3, 4, 5, 6, 7, 8]

A comprehension would be useful if the list were more complex:

nums = [1, 2, 3, 4, 5, 2, 1, 5, 6, 7, 8, 1]
animals = ['goats', 'ducks', 'sheep', 'dogs', 'sheep', 'goats', 'cats', 'ducks', 'sheep', 'cats', 'sheep', 'dogs']

num_animals = list(zip(nums, animals))
print(num_animals) #[(1, 'goats'), (2, 'ducks'), (3, 'sheep'), (4, 'dogs'), (5, 'sheep'), (2, 'goats'), (1, 'cats'), (5, 'ducks'), (6, 'sheep'), (7, 'cats'), (8, 'sheep'), (1, 'dogs')]

unique_animals = list({a for n, a in num_animals})
print(unique_animals) #['cats', 'dogs', 'ducks', 'goats', 'sheep']

Happy coding!

1 Like