Python all() Function β Easy Explanation with Examples
How does the "all" function in Python work? - Stack Overflow
Is there a list of every Python function out there?
How to use all() in python? - Stack Overflow
Videos
only when all the elements are Truthy.
Truthy != True.
all essentially checks whether bool(something) is True (for all somethings in the iterable).
>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True
'?' and '!' are both truthy since they are non-empty Strings.
There's a difference between True and "truthy". Truthy means that when coerced, it can evaluate to True. That's different from it being == to True though.
I recently learnt that stuff like .strip(), .title(), exists, which got me interested to learn about all these little bonus gimmicks about Python that I could use in my everyday tasks or to improve functionality in my code.
As of now I know that https://docs.python.org/3/library/functions.html exists but there isn't the functions mentioned above within that list. Is there another list of all these functions including those mentioned out there? Thank you
If the goal is:
Check that all elements of a list are not present in a string.
The pattern should be: all(s not in my_string for s in input_list)
l = ["abc","ghi"]
s1 = "xyzjkl"
s2 = "abcdef"
print(all(s not in s1 for s in l)) # True
print(all(s not in s2 for s in l)) # False
You need a list of True, False. But you were simply getting the matched items, so when you do an all on Truthy values you will get True. Instead do:
all([x not in s1 for x in l])
all([x not in s2 for x in l])
or just without list comp, because all accepts an iterable.
all(x not in s1 for x in l)
all(x not in s2 for x in l)