pythonpython-3.xlistfor-loopwhile-loop

Counting the number of occurrences of a number in a list without using loops (for or while)


For example there is a list like A = [7 , 77 , 777 , 717]. I want to count the number of occurrences of the digit 7 in the list, but without using any loops. It should output 8. (7 occurred 8 times). Does anyone know how to do this? I tried to do it by loops but couldn't find a way to do it without any loops (if it's even possible).


Solution

  • One way is to just convert it into string and count

    A = [7 , 77 , 777 , 717]
    
    str(A).count('7')
    
    #output
    8