Check if all elements in a list are identical

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

    You can do:

    reduce(and_, (x==yourList[0] for x in yourList), True)
    

    It is fairly annoying that python makes you import the operators like operator.and_. As of python3, you will need to also import functools.reduce.

    (You should not use this method because it will not break if it finds non-equal values, but will continue examining the entire list. It is just included here as an answer for completeness.)

提交回复
热议问题