Is it possible to use ‘else’ in a list comprehension? [duplicate]

Here is the code I was trying to turn into a list comprehension:

table=""
for index in xrange(256):
    if index in ords_to_keep:
        table += chr(index)
    else:
        table += replace_with

Is there a way to add the else statement to this comprehension?

table="".join(chr(index) for index in xrange(15) if index in ords_to_keep)

6 Answers
6

Leave a Comment