Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I condense this simple "if" line of code?

Tags:

python

I have a line of code in my program that checks if a number is divisible by several numbers.

However, the way I currently have it is extremely inefficient and ugly.

It currently looks like:

if(x % 1 == 0 and x % 2 == 0 and x % 3 == 0) and so forth for several other numbers. I was hoping someone could teach me how I could take out the long and chain and help me replace it with something that makes more sense so instead it will look like:

if(x % a[1,2,3] == 0)

Is this possible and if so can anyone please help me?

EDIT: The x % 1 == 0, x % 2 == 0, etc.. Is not the full equation. I am checking far more than % 1,2, and 3. I am looking for a solution that can take say.. 1 through 15. That is why I am not using x % 6.

like image 878
Vale Avatar asked Dec 02 '22 16:12

Vale


1 Answers

if all(x % k == 0 for k in [1, 2, 3]):
    print 'yay'
like image 187
Paul Hankin Avatar answered Dec 24 '22 08:12

Paul Hankin