Scott Miller πΊπ¦ πΊπΈ on Nostr: Python Question: Is there an efficient way to see if every element in an iterable is ...
Python Question: Is there an efficient way to see if every element in an iterable is equal to a user-specified value?
The best idea I've seen so far is to use a list comprehension to check for equality, and then use the all function, like this:
MyList = [1,1,1,1,1,1,1]
all((x==1 for x in MyList))
That's readable and probably not bad performance. Is there something better?
#Python #PythonQuestion
The best idea I've seen so far is to use a list comprehension to check for equality, and then use the all function, like this:
MyList = [1,1,1,1,1,1,1]
all((x==1 for x in MyList))
That's readable and probably not bad performance. Is there something better?
#Python #PythonQuestion