inbinder
inbinder

Reputation: 722

list comprehension and exclusion in one value

I'm having an issue in python3. I want to exclude 0 as one of the values, however I can't seem to make that happen. I attempted adding another "and if" parameter but the result still included 0.

I want the values that are multiples of 6 between 1 and 100. 0 is clearly not one of them.

x for x in range(100) if x % 6 == 0 

Upvotes: 0

Views: 2828

Answers (3)

Danica
Danica

Reputation: 28856

The most direct answer is just

range(6, 101, 6)

But your original approach should work if you do something like:

[x for x in range(101) if x % 6 == 0 and x != 0]

(no and if needed, it's just a single if clause with a compound test).

Upvotes: 2

njzk2
njzk2

Reputation: 39405

If you want to exclude 0, don't include it in the first place :

range(1, 100)

Upvotes: 1

Kos
Kos

Reputation: 72319

Try:

x for x in range(100) if x%6 == 0 and x != 0

or simply:

x for x in range(1,100) if x%6 == 0

Upvotes: 1

Related Questions