I want to do something like this:
a = some_funct() b = [ 1, a if a is not None ]
List b must be one long element if a is None, and two elements are long if a is not None. Is this possible in Python, or do I need to use a separate one if I check and then add ()?
Doesn't look better, but you could do
b = [1] + ([a] if a is not None else [])
Of course, it would be better to check how this improves code readability.
a = some_funct() b = [ 1, a ] if a is not None else [1]
,
b = [x for x in (1, a) if x is not None]
(1, a) - , b , None
(1, a)
None
I relate to this game too late, but I had the same question, and, seeing other answers, I came up with this solution using sets:
sets
list({1, a or 1})
Source: https://habr.com/ru/post/1661136/More articles:Bottom view - androidEnqueue and increment variable in Tensor Flow - pythonNSPanel removed in Xcode 8? - cocoaForced copy of the tensor when pasting - tensorflowHow to get query string in Polymer - javascriptCan I move elements out of range? - c ++в соответствии с последовательностями и IteratorProtocol в Swift - swiftPartial upgrade on postgres upsert violates restriction - postgresqlwhy histria or any other circuit breaker for microservice? - javahow to disable specific date range in bootstrap daterangepicker? - jqueryAll Articles