Check if list contains only item x

后端 未结 8 1145
长发绾君心
长发绾君心 2020-12-17 17:03

Say all of w, x, y, and z can be in list A. Is there a shortcut for checking that it contains only x--eg. without negating the other variables?

w, x, y, and z

8条回答
  •  醉酒成梦
    2020-12-17 17:21

    This checks that all elements in A are equal to x without reference to any other variables:

    all(element==x for element in A)
    

提交回复
热议问题