S.M.
S.M.

Reputation: 1

Python: "or" in short form

Is there a possibility to write this in a shorter way?

elif line.startswith('\tVLRLIST=') and (line.rstrip()[9:-1] == '27' or line.rstrip()[9:-1] == '28' or line.rstrip()[9:-1] == '29')

Upvotes: 0

Views: 164

Answers (3)

Rafael
Rafael

Reputation: 7242

I would use a different approach and try to name these, it's much more readable.

def prefix_is_correct(line):
    return line.startswith('\tVLRLIST=')

def is_valid(line):
    return prefix_is_correct(line) and line.rstrip()[9:-1] is in ['27', '28', '29']

.
.
.

elif is_valid(line):

Upvotes: 0

Amadan
Amadan

Reputation: 198334

elif re.match(r'^\tVLRLIST=2[7-9]\s*$', line):

Upvotes: 3

BlueSheepToken
BlueSheepToken

Reputation: 6109

As a general case, you can use the in keyword,

elif line.startswith('\tVLRLIST=') and line.rstrip()[9:-1] in ('27', '28', '29'):

For this specific example, all of them are contained in a range:

elif line.startswith('\tVLRLIST=') and (int(line.rstrip()[9:-1]) in range(27, 30):

Upvotes: 3

Related Questions