Why is this simple conditional expression not working? [duplicate]

You are using it wrong. Use it this way:

a = 2 if i in [1, 3, 6] else 7

The general form is:

var = val1 if cond else val2

Leave a Comment