I have an assignment here:
Given an array of ints, return the number of 9's in the array.
array_count9([1, 2, 9]) → 1
array_count9([1, 9, 9]) → 2
array_count9([1, 9, 9, 3, 9]) → 3
I have 2 ideas for this, one is:
def array_count9(nums):
count = 0
list1 = [x for x in nums if x==9]
return len(list1)
and the other:
def array_count9(nums):
count = 0
for n in nums:
if n==9:
count +=1
return count
But I wonder which way would be more Pythonic, in terms of performance, clarity,... ? Thank you very much