Check if all elements in a list are identical

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

    Doubt this is the "most Pythonic", but something like:

    >>> falseList = [1,2,3,4]
    >>> trueList = [1, 1, 1]
    >>> 
    >>> def testList(list):
    ...   for item in list[1:]:
    ...     if item != list[0]:
    ...       return False
    ...   return True
    ... 
    >>> testList(falseList)
    False
    >>> testList(trueList)
    True
    

    would do the trick.

提交回复
热议问题