I have a function prime(x) that returns True if x is prime and False if x is false.
Is there an efficient way to iterate through a list and if all the members satisfy the function, return True, and otherwise return false?
For the prime example, I wrote:
def primecheck(x):
for index in xrange(0,len(x)):
if prime(x[index])==False:
return False
break
return True
but I imagine that this is inefficient and there must be a much better way of doing it.
Is there a standard method for iterating a generic function (where I define a generic function as something that evaluates an integer or string to be True or False) through a list without having to do something like the above each time? Or even if there isn't a standard method, is there a more efficient method than running through the index of the list?