Check if all elements in a list are identical

前端 未结 22 1968
死守一世寂寞
死守一世寂寞 2020-11-22 07:45

I need a function which takes in a list and outputs True if all elements in the input list evaluate as equal to each other using the standard equal

22条回答
  •  情话喂你
    2020-11-22 07:53

    This is another option, faster than len(set(x))==1 for long lists (uses short circuit)

    def constantList(x):
        return x and [x[0]]*len(x) == x
    

提交回复
热议问题