4

Is ternary iteration possible? A simplistic version of what I mean, though this particular example could be done in a better way:

c = 0  
list1 = [4, 6, 7, 3, 4, 5, 3, 4]  
c += 1 if 4 == i for i in list1 else 0  

A more practical example:

strList = ['Ulis', 'Tolus', 'Utah', 'Ralf', 'Chair']
counter = 0  
counter += 1 if True == i.startswith('U') for i in strList else 0  
return counter  
Jack J
  • 1,514
  • 3
  • 20
  • 28

3 Answers3

6

Your "practical example" is written as:

>>> strList = ['Ulis', 'Tolus', 'Utah', 'Ralf', 'Chair']
>>> sum(1 for el in strList if el.startswith('U'))
2

Your other example (if I understand correctly) is:

>>> list1 = [4, 6, 7, 3, 4, 5, 3, 4]
>>> list1.count(4)
3

(or just adapt the strList example, but nothing wrong with using builtin methods)

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

@Jon Clements gave you an excellent answer: how to solve the problem using Python idiom. If other Python programmers look at his code, they will understand it immediately. It's just the right way to do it using Python.

To answer your actual question: no, that does not work. The ternary operator has this form:

expr1 if condition else expr2

condition must be something that evaluates to a bool. The ternary expression picks one of expr1 and expr2 and that's it.

When I tried an expression like c += 1 if condition else 0 I was surprised it worked, and noted that in the first version of this answer. @TokenMacGuy pointed out that what was really happening was:

c += (1 if condition else 0)

So you can't ever do what you were trying to do, even if you put in a proper condition instead of some sort of loop. The above case would work, but something like this would fail:

c += 1 if condition else x += 2  # syntax error on x += 2

This is because Python does not consider an assignment statement to be an expression.

You can't make this common mistake:

if x = 3:  # syntax error!  Cannot put assignment statement here
    print("x: {}".format(x))

Here the programmer likely wanted x == 3 to test the value, but typed x = 3. Python protects from this mistake by not considering an assignment to be an expression.

You can't do it by mistake, and you can't do it on purpose either.

steveha
  • 74,789
  • 21
  • 92
  • 117
  • 2
    `c += 1` isn't an expression, it's an assignment. The statement will parenthesize as `c += (truthy if cond else falsey)`. You could *not* use the `c += 1` anywhere else, for instance `truthy if cond else c += 1` parenthesizes as `(truthy if cond else c) += 1`, which is rejected because you can't assign to a conditional expression. – SingleNegationElimination Feb 01 '13 at 03:09
  • Ahh, you are right! I was surprised it worked, but I didn't think enough about what was really going on. Well, time to edit my answer. Thank you for correcting me. – steveha Feb 01 '13 at 03:31
0

You can also select your items with list comprehension, and take number of elements in list.

strList = ['Ulis', 'Tolus', 'Utah', 'Ralf', 'Chair']
len([k for k in strList if k.startswith('U')])
kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • 1
    If you want to use len() then you must resort to a list comprehension, isnt it? or at least, this is a way to do it – kiriloff Feb 01 '13 at 02:57
  • @antitrust I think @Jon is saying that `sum(generator)` is preferred to `len(list)`, because you don't have to build up a list then count it – Alex L Feb 01 '13 at 03:19
  • @antitrust i.e. `sum(1 for k in strList if k.startswith('U'))` – Alex L Feb 01 '13 at 03:20
  • @AlexL Yes - unless you need a list after - don't materialise one for the sake of it – Jon Clements Feb 01 '13 at 03:32