bool value of a list in Python

后端 未结 4 563
名媛妹妹
名媛妹妹 2021-01-18 05:45

what is the best way to turn a list into bool value? I am looking for something like:

return eval_bool(my_list)

I have a custom container i

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 06:23

    If len(my_list) == 0 it is returned as false, otherwise it is true. It is completely pythonic to write:

    return len(my_list)
    

    which although it is returned as an integer, evaluates as true for non zero lengths, and false otherwise.

提交回复
热议问题