-3

I keep solving coding challenges and am able to do them quite simply. But the problem arises once I try to compact the code into a style that uses ternary operators. In this problem, I am trying to find the integer that occurs an odd amount of times in an array. I do this in the normal, good practice form and everything is fine.

def find_it(seq):
for x in seq:
    if seq.count(x) % 2 == 1:
        return x

This works but when I try to turn it into something like this:

def find_it(seq):
for x in seq:
    return x if seq.count(x) % 2 == 1

It gives this error almost every time I try ternary operators:

File "<string>", line 10
return x if seq.count(x) % 2 == 1
                                ^
SyntaxError: invalid syntax

What is the issue? Why does it always say invalid syntax?

  • Basically Andrey is saying that there needs to be a condition, something to do if true, and something to do if false. –  Apr 01 '18 at 02:04
  • 1
    You need an `else` in your example. – JacobIRR Apr 01 '18 at 02:06
  • Ok, I enter this: `return x if seq.count(x) % 2 == 1 else None` and I still get the issue `None should equal 5`. I wouldn't assume the _else_ to be the problem. – cryptofanatic09 Apr 01 '18 at 02:08
  • Try: `return x if seq.count(x) % 2 == 1 else None`. It reads as: `return (x if seq.count(x) % 2 == 1 else None)` Remember that if a function doesn't return anything it returns `None` by default. If you use a ternary operator you have to make this explicit. – jpp Apr 01 '18 at 02:09
  • @cryptofanatic09 I think the root of the issue is that you're thinking like a human instead of a computer (which is generally to be commended): you seem to be mixing up _expressions_ and _statements_: `return` can only appear as a _statement_, and a ternary expression is an _expression_. When the interpreter sees `return`, it expects only an expression, and the ternary expression requires an `else` clause. See https://docs.python.org/3.6/reference/grammar.html for the painful grammar details. – Cody Piersall Apr 01 '18 at 02:19

2 Answers2

0

This would be valid:

def find_it(seq):
    for x in seq:
        return x if seq.count(x) % 2 == 1 else None

But that's not what you want, is it? It seems your original working code is the best solution.

The return x if CONDITION syntax you were trying to use does work in Ruby, but not in Python. There's no general "trailing if" support in Python like there is in Ruby (and is not in most other languages).

John Zwinck
  • 207,363
  • 31
  • 261
  • 371
0

Does Python have a ternary conditional operator?

Ternarys have a "answer if condition else other answer" format in python.

Seems like you don't have a return condition if x is not in the sequence so maybe return 0??

return x if seq.count(x) % 2 == 1 else 0
Vilyanare
  • 44
  • 2
  • No. This would just return 0 if the first element occurs an even number of times. It doesn't seem to make any sense to return unconditionally in the first iteration. – Andrey Tyukin Apr 01 '18 at 02:22