Using ternary operator in python?

TheChetan

Consider the following code snippet. It flags a syntax error at the break statement.

digits = list(str(102))
dummy = list(str(102/2))
for j in digits:
    dummy.remove(j) if j in dummy else break

How do I fix this?(I want to still use the ternary operator)

Ian

Edit:

(see my conversation with Stefan Pochmann in the comments)

Ternary operator is not for only statement, but rather for assignment or for expression (and break is an only statement):

a = 5 if cond else 3 #OK
do() if cond else dont() #also OK
do() if cond else break #not OK

use normal if-else statement to do statements:

if cond:
    do()
else:
    break

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using of ternary operator in view

From Dev

Using the ternary operator in a macro

From Dev

Nesting the ternary operator in Python

From Dev

Ternary operator on arrays in python

From Dev

"Do nothing" using ternary operator

From Dev

Using pattern matching with ternary if operator

From Dev

Using a concatenation operator with a ternary conditional?

From Dev

Assignment to expression using ternary operator

From Dev

Am I using the ternary operator?

From Dev

Using ternary operator to calculate the length

From Dev

Python ternary operator and assignment in else

From Dev

Python: Ternary operator syntax error

From Dev

Recursion with ternary operator 'hack' in Python

From Dev

Python ternary operator failure on dictionary

From Dev

Python - loop inside ternary operator

From Dev

How is ternary operator implemented in Python

From Dev

Recursion with ternary operator 'hack' in Python

From Dev

Python - loop inside ternary operator

From Dev

Python: Ternary operator syntax error

From Dev

Using continue in python ternary?

From Dev

Using Ruby's ternary operator ? : to shorten this

From Dev

C function call selection using ternary operator

From Dev

Ternary operator, syntax error when using assignment

From Dev

TCL conditional commands using ternary operator

From Dev

Assign null to decimal using ternary operator

From Dev

Performance difference using std::min or ternary operator?

From Dev

Using ternary operator on Console.WriteLine

From Dev

Using block with ternary operator in Objective-C

From Dev

How to use AND in if statement using ternary operator