how to count the appearance of number or string in a list python 1

how to count the appearance of number or string in a list python

def count_apperace(list,number_or_string_to_find):
    count = 0 
    for item in list:
        if item == number_or_string_to_find:
            count += 1
    return count

print("the number 3 has occured ",count_apperace([1,27,53,6,74,2,3,4,3,77,8],3),"times")

Here is what the above code is Doing:
1. We define a function called count_apperace that takes two arguments: a list and a number or string to find.
2. We create a variable called count and set it to 0.
3. We loop through the list.
4. If the item in the list is equal to the number or string we’re looking for, we add 1 to count.
5. We return the count.
6. We call the function and pass in a list and a number or string to find.

Similar Posts