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

后端 未结 8 1940
离开以前
离开以前 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 02:15

    All you need is

    len(listName)
    

    Which will give you the length.

    I guess you could also do this then

    if len(listName) % 2 == 0:
        return True  # the number is even!
    else:
        return False # the number is odd!
    

提交回复
热议问题