8

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)

8
  • 6
    break is a statement, and as such it can't be used inside a ternary. Sorry. Commented May 13, 2016 at 15:50
  • I don't think you can use ternary operator for that case... Commented May 13, 2016 at 15:50
  • To remove an element from a list you probably need to be careful (safer is to do the reverse way). check this out: stackoverflow.com/questions/35618307/… see also Padraic's answer Commented May 13, 2016 at 16:22
  • 1
    @MarkRansom That's wrong reasoning, as expressions are statements and they can be used inside a ternary. Commented May 13, 2016 at 16:24
  • 4
    @StefanPochmann: Expressions are statements - but not all statements are expressions (break being an example), and the ternary operator requires expressions (not statements). Commented May 13, 2016 at 16:29

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

16 Comments

Your examples aren't quite the same, as they are valid syntax
@TheChetan: no, there isn't. Python focuses on readability, not conciseness.
@TheChetan hmm... depends on the case, you could in fact make for loop and store the functions in list first for instance
@sparkandshine ah I get it... that is because exit is a function. If you use tool like PyCharm you can see that.. it is like function returning nothing
@Ian Ah, ok. The problem is that they're only statements, not expressions, and you need an expression there.
|
3

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.

2 Comments

Ugh, it's awkward to avoid the dunder method here; you can use functools.partial and operator.contains, e.g. in_dummy = partial(contains, dummy). This seems like a lot of work to avoid the underscore method though, and no real benefits. A lambda could be used alternatively, e.g. lambda e: e in dummy.
@JaredGoguen, yep, I don't mind using __contains__ in a case like this, definitely better than a lambda, might be interesting to time the comparisons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.