I want to join a list with a conditional statement such as:
str = "\n".join["a" if some_var is True, "b", "c", "d" if other_var==1, "e"]
Each element has a different conditional clause (if at all) so a normal list comprehension is not suitable in this case.
The solution I thought of is:
lst = ["a" if some_var is True else None, "b", "c", "d" if other_var==1 else None, "e"]
str = "\n".join[item for item in lst if item is not None]
If there a more elegant pythonic solution?
Thanks,
Meir
More explanation:
In the above example, if some_var equals to True and other_var equals to 1 I would like to get the following string:
a
b
c
d
e
If some_var is False and other_var equals to 1 I would like to get the following string:
b
c
d
e
If some_var is True and other_var is not equals to 1 I would like to get the following string:
a
b
c
e
I think this is what you want: