Python Ternary Operator Without else

后端 未结 7 860
执笔经年
执笔经年 2020-11-29 00:36

Is it possible to do this on one line in Python?

if :
    myList.append(\'myString\')

I have tried the ternary operator:

7条回答
  •  没有蜡笔的小新
    2020-11-29 01:02

    myList.extend(['myString'] if condition else []) would also work, though it's more work than the other solutions.

提交回复
热议问题