Check if all elements in a list are identical

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

    Regarding using reduce() with lambda. Here is a working code that I personally think is way nicer than some of the other answers.

    reduce(lambda x, y: (x[1]==y, y), [2, 2, 2], (True, 2))
    

    Returns a tuple where the first value is the boolean if all items are same or not.

提交回复
热议问题