I am trying to implement a ternary conditional operator in a list-comprehension. I have written it like this:
lst.append(dict2obj(item)) if type(item) is not in ['int'] else lst.append(item) for item in v
Where lst
is empty list and v
is another list with various elements. Editor is showing it syntactically incorrect. What am I doing wrong?
[
, ]
:is not in
operator. Use not in
.type
function does not return string
. Why not use isinstance(item, int)
?[lst.append(dict2obj(item)) if not isinstance(item, int) else lst.append(item)
for item in v]
Use simple for
loop if possible. It's more readable.
for item in v:
if not isinstance(item, int)
lst.append(dict2obj(item))
else:
lst.append(item)
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