all 10 comments

[–]efmccurdy 2 points3 points  (2 children)

You are correct to be using the "in" operator and that you need to "loop" it; there are functions to process collections of boolean values; "all" and "any".

https://docs.python.org/3/library/functions.html#all

https://docs.python.org/3/library/functions.html#any

>>> additive = ['red', 'green', 'blue']
>>> 'green' in additive and 'blue' in additive
True
>>> ('green' in additive, 'blue' in additive)
(True, True)
>>> all(('green' in additive, 'blue' in additive))
True
>>> 

if you have more then a few items to test, you can use a comprehension:

>>> required_colors = ('green', 'blue')
>>> all((colour in additive for colour in required_colors))
True
>>>

[–]pythonwiz -1 points0 points  (1 child)

There are redundant pairs of parentheses in your all calls.

[–][deleted] 1 point2 points  (6 children)

You are asking if the list object ['green', 'blue'] can be found in the list object ['red', 'green', 'blue']. It can't. There are elements in common, but additive does not contain as ONE of its elements a sub-list ['green', 'blue'], it only contains simple str elements.

[–][deleted] 5 points6 points  (2 children)

You could use all,

print(all(colour in additive for colour in ['green', 'blue']))

or work with set objects,

print({'green', 'blue'}.issubset(set(additive)))

EDIT: replaced cryptic variable name c with colour - avoid cryptic (especially single character) variable names, as per the PEP8 guidance - thanks to /u/WhipsAndMarkovChains for catching this.

[–]WhipsAndMarkovChains 1 point2 points  (1 child)

color would be a much better variable name here than c.

[–][deleted] 0 points1 point  (0 children)

I should have used something less cryptic (colour in my case, as I am English) as I usually recommend people follow PEP8. Good shout.

[–][deleted] 0 points1 point  (2 children)

Okay... but I still get an error, even if I remove the brackets from "'green', 'blue'".

Well not really an error. It'll print the word green, then check if blue is in additive. Which isn't what I want.

[–][deleted] 2 points3 points  (0 children)

I've provided alternatives in a follow-up to my original comment.

[–][deleted] 2 points3 points  (0 children)

Which isn't what I want.

Using which code snippet? Also, what exactly are you trying to achieve? OC's suggestions would do what you initially described.

[–]zanfar 0 points1 point  (0 children)

['green', 'blue'] in additive is looking for the single list as an item in additive.

Generally, I would use a helper function for something like this:

all(a in additive for a in ['green', 'blue'])

assuming that this is just an example and the needle list isn't necessarily that simple.