I want to do something like this:
a = some_funct()
b = [ 1, a if a is not None ]
The list b should be one element long if a is None, and two elements long if a is not None. Is this possible in Python or do I have to use a separate if check followed by add()?
Doesn't look the best, but you could do
b = [1] + ([a] if a is not None else [])
Of course, it would be better to check as it increases code readability.
You can do this using list comprehension
b = [x for x in (1, a) if x is not None]
The tuple (1, a)
is the total set, b will become a list of all elements in that total set which are not None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With