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)
The syntax
a if b else cis a ternary operator in Python that evaluates toaif the conditionbis true – otherwise, it evaluates toc. It can be used in comprehension statements:So for your example,