Can I optionally include one element in a list without an else statement in python?
Use concatenation:
x = ([1] if conditional else []) + [3, 4]
In other words, generate a sublist that either has the optional element in it, or is empty.
Demo:
>>> conditional = False
>>> ([1] if conditional else []) + [3, 4]
[3, 4]
>>> conditional = True
>>> ([1] if conditional else []) + [3, 4]
[1, 3, 4]
This concept works for more elements too, of course:
x = ([1, 2, 3] if conditional else []) + [4, 5, 6]
You can do it with a slice
x = [1, 3, 4][not conditional:]
eg
>>> conditional = False
>>> [1, 3, 4][not conditional:]
[3, 4]
>>> conditional = True
>>> [1, 3, 4][not conditional:]
[1, 3, 4]