`elif` in list comprehension conditionals `elif` in list comprehension conditionals python python

`elif` in list comprehension conditionals


Python's conditional expressions were designed exactly for this sort of use-case:

>>> l = [1, 2, 3, 4, 5]>>> ['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]['yes', 'no', 'idle', 'idle', 'idle']


>>> d = {1: 'yes', 2: 'no'}>>> [d.get(x, 'idle') for x in l]['yes', 'no', 'idle', 'idle', 'idle']


You can, sort of.

Note that when you use sytax like:

['yes' if v == 1 else 'no' for v in l]

You are using the ternary form of the if/else operator (if you're familiar with languages like C, this is like the ?: construct: (v == 1 ? 'yes' : 'no')).

The ternary form of the if/else operator doesn't have an 'elif' built in, but you can simulate it in the 'else' condition:

['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]

This is like saying:

for v in l:    if v == 1 :        print 'yes'    else:        if v == 2:            print 'no'        else:            print 'idle'

So there's no direct 'elif' construct like you asked about, but it can be simulated with nested if/else statements.