2

I am learning python, and trying to use some ternary operators.

I am trying to make the function below using a ternary:

def array_count9(nums): count = 0 for i in nums: if i == 9: count += 1 return count 

I have tried:

def array_count9(nums): count = 0 count += 1 if i == 9 for i in nums else pass return count 

which threw a SyntaxError, then after looking around I found this and changed my code for what I believed was better ordering:

def array_count9(nums): count = 0 count += 1 if i == 9 else pass for i in nums return count 

Still receiving SyntaxError which is pointing at the for. I've tried using parentheses in different places too.

I have looked around and there are other related threads such as this and this, which resulted in me trying this:

def array_count9(nums): count = 0 count += 1 if i == 9 else count == count for i in nums return count 

I've also tried other resources by searching Google, but I can't quite work it out. Please teach me.

Thanks

3 Answers 3

2

I think this is the most idiomatic way to write the code:

def array_count9(nums): return sum(num == 9 for num in nums) 

But you could also do this if you want to use the if/else construct:

def array_count9(nums): return sum(1 if num == 9 else 0 for num in nums) 
Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks, this works, however I still don't understand why what I've used isn't working. Can you explain?
@JakeStokes I'm not sure how to explain... I think all of your attempts were simply syntax errors? List comprehension in Python results in an iterator of values... there's nothing like what you were trying to do.
Hmm.. there seem to be people doing what I'm trying to do here but I can't make it work. Can you confirm that I'm doing something different?
There's nothing wrong with 1 if foo else 2. (That's in my second solution.) It was the loop that didn't really make sense.
1

The blueprint for a ternary operator is:

condition_is_true if condition else condition_is_false 

The statement where the syntax error occurs is at

count += 1 if i == 9 else pass for i in nums 

ie count += 1 does not meet the blueprint specification, because condition_is_true should not need to be evaluated.

1 Comment

So after wasting a lot of time, you've helped me understand why it wouldn't work. Thanks a lot @nnja
0

Okay so using your examples was a little tricky because a ternary operator can't include anything outside it's specific blueprint; that being the for loop you're trying to pass with it.

count += 1 if i == 9 for i in nums else pass 

So after fiddling around with the code:

def array_count9(nums): count = 0 count += 1 if i == 9 for i in nums else pass return count 

I was sure you were looking for something that works involving ternary operators and that for loop. So keeping your goal in mind, this is what I came up with.

numss = [3,6,9,10,35] def count9(nums): count = 0 a = count + 1 for i in nums: count = (a if i == 9 else count) #This being the ternary operator return count print (count9(numss)) 

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.