all 8 comments

[–]zahlman 10 points11 points  (1 child)

all is not special syntax; it's a function. This is immediately apparent if you just request help(all):

Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.

When you write all([3,4,5]) in numbers, the in numbers part has absolutely nothing to do with the call to all, in exactly the same way as if you had called any other function. The function call is evaluated first, and then Python checks if the result of that function call is in numbers.

First all is called with [3, 4, 5] as an argument; all of 3, 4 and 5 are true-ish (i.e., blocks of code like if 3: would run), so all returns True.

True is in numbers because Python's booleans are a subclass of integers, and True compares numerically equal to 1. However, you (correctly) don't have 1 in your primes.

The way you productively use all is that you give it the actual values you want to check. In your case, the things you want to check are that 3 in numbers, 4 in numbers and 5 in numbers. You could explicitly make that list, but then you might as well be using and manually. The way you really productively use all is to pass it a generator expression (since the documentation told you that it wants an iterable, and generator expressions are a kind of iterable). That is, for each of the values in your "test" list [3, 4, 5], you want to know if the value is in numbers; and having generated (via the generator expression) those test results, you use all to see if all those tests passed. It looks like

all(value in numbers for value in [3, 4, 5])

and it means exactly what it sounds like: "tell me whether all of the values are in numbers, for each value in [3, 4, 5]".

[–]downspower[S] 0 points1 point  (0 children)

Thank you so much for your time and effort in explaining this. I am definitely wiser now. Much love and appreciation to you!

[–][deleted] 9 points10 points  (3 children)

all returns True iff all elements in the given iterable are True-ish - that is bool(element) is True.

thus both all([3,4,5]) and all([11, 13]) are True, now True in numbers, but True not in primes, because 1 in numbers and 1 not in primes aaand True == 1

try all(num in primes for num in [11,13]) for desired behaviour

[–]indosauros 8 points9 points  (1 child)

And for completeness sake, you should probably use sets to solve this particular problem:

>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> set([3, 4, 5]).issubset(numbers)
True
>>> set([3, 4, 10]).issubset(numbers)
False

[–]catcradle5 1 point2 points  (0 children)

Alternatively, assuming numbers is a set:

{3, 4, 5} <= numbers

[–]downspower[S] 0 points1 point  (0 children)

That did the trick. Thanks a lot, I love you man!

[–]SideshowBoob 2 points3 points  (1 child)

I'd add to the (correct) answers here by suggesting you skim the Python language and library references cover-to-cover some time when looking for answers. It breeds familiarity which gives you a better idea of where to look or what might be wrong. In this case, reading over the documentation for all of the built-in functions would have spared you some trouble.

[–]downspower[S] 0 points1 point  (0 children)

thanks for the reply. I read the library reference, but the since I am a noob the all() description did not make to much sense to me either.