How to know if a list has an even or odd number of elements

后端 未结 8 2010
离开以前
离开以前 2021-01-28 01:20

How can I find out if there is even, or odd, number of elements in an arbitrary list.

I tried list.index() to get all of the indices... but I still don\'t k

8条回答
  •  甜味超标
    2021-01-28 01:57

    def has_even_length(some_sequence):
        return not len(some_sequence)%2
    
    def has_odd_length(some_sequence):
        return bool(len(some_sequence)%2)
    

提交回复
热议问题