I manipulating big list in my program, so what's the fastest way to know if a list is filled with a same number ? And I'm not able to use module.
As example :
>>> isFilled([1,1,1,1,1])
True
>>> isFilled([5,5,5,5])
True
>>> isFilled([1,6,5,1,1])
False
I have done this, but it's create a new list and that's a problem as I manipulating big list :
def isFilled(lst):
return [lst[0]]*len(lst) in lst
Here: all(x==lst[0] for x in lst)
. (x==lst[0] for x in lst)
is a generator comprehension. all()
returns True
if all the elements of an iterator (a generator comprehension is an iterator) return True
when passed to bool()
, which converts a value into a boolean value (True
or False
). Note that it evaluates as False
immediately after a different element is spotted.