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
This checks that all element
s in A
are equal to x
without reference to any other variables:
all(element==x for element in A)
A=[w,y,x,z]
all(p == x for p in A)
I'm not sure what without negating the other variables means, but I suspect that this is what you want:
if all(item == x for item in myList):
#do stuff
{x} == {w,x,y,z} & set(A)
This will work if all of [w,x,y,z]
and items in A
are hashable.
That, or if you don't want to deal with a loop:
>>> a = [w,x,y,z]
>>> a.count(x) == len(a) and a
(and a
is added to check against empty list)
If all items in the list are hashable:
set(A) == set([x])