Check if all elements in a list are identical

前端 未结 22 2216
死守一世寂寞
死守一世寂寞 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:47

    There is also a pure Python recursive option:

    def checkEqual(lst):
        if len(lst)==2 :
            return lst[0]==lst[1]
        else:
            return lst[0]==lst[1] and checkEqual(lst[1:])
    

    However for some reason it is in some cases two orders of magnitude slower than other options. Coming from C language mentality, I expected this to be faster, but it is not!

    The other disadvantage is that there is recursion limit in Python which needs to be adjusted in this case. For example using this.

提交回复
热议问题