Check if all elements in a list are identical

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

    You can use .nunique() to find number of unique items in a list.

    def identical_elements(list):
        series = pd.Series(list)
        if series.nunique() == 1: identical = True
        else:  identical = False
        return identical
    
    
    
    identical_elements(['a', 'a'])
    Out[427]: True
    
    identical_elements(['a', 'b'])
    Out[428]: False
    

提交回复
热议问题