Kian Mashayekhi
Kian Mashayekhi

Reputation: 3

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).

Upvotes: -4

Views: 109

Answers (1)

Talha Tayyab
Talha Tayyab

Reputation: 27800

One way is to just convert it into string and count

A = [7 , 77 , 777 , 717]

str(A).count('7')

#output
8

Upvotes: 8

Related Questions