Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ternary operator in python?

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)

like image 458
TheChetan Avatar asked May 13 '16 15:05

TheChetan


People also ask

How do you use a ternary operator in Python?

Similarly the ternary operator in python is used to return a value based on the result of a binary condition. It takes binary value(condition) as an input, so it looks similar to an “if-else” condition block. However, it also returns a value so behaving similar to a function.

Does Python have a ternary operator?

The ternary operator is a way of writing conditional statements in Python. As the name ternary suggests, this Python operator consists of three operands. The ternary operator can be thought of as a simplified, one-line version of the if-else statement to test a condition.

How do you use a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What is Python ternary operator symbol?

Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.


2 Answers

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
like image 155
Ian Avatar answered Oct 27 '22 01:10

Ian


You cannot use break in Your loop logic can be re written using itertools.takewhile if you want a more succinct solution

digits = list(str(102))
dummy = list(str(102/2))

from itertools import takewhile

for d in takewhile(dummy.__contains__, digits):
    dummy.remove(d)

You can also remove the need for the else using a for loop by reversing your logic, check if j is not in dummy breaking when that is True:

for j in digits:
    if j not in dummy:
        break
    dummy.remove(j)

Also if you want to remove all occurrences of any of the initial elements from digits that are in dummy, remove won't do that for any repeating elements but using a list comp after creating a set of elements to remove will:

digits = str(102)
dummy = list(str(102/2))
st = set(takewhile(dummy.__contains__, digits))
dummy[:] = [d for d in dummy if d not in st]

print(dummy)

You can also iterate over a string so no need to call list on digits unless you plan on doing some list operations with it after.

like image 36
Padraic Cunningham Avatar answered Oct 26 '22 23:10

Padraic Cunningham