
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Search the Number of Times a Particular Number Occurs in a List
When it is required to search the frequency of a number in a list, a method is defined, that takes a list and the number. It iterates through the list, and every time the number is encountered, the counter is incremented.
Below is the demonstration of the same −
Example
def count_num(my_list, x_val): my_counter = 0 for elem in my_list: if (elem == x_val): my_counter = my_counter + 1 return my_counter my_list = [ 66, 26, 48, 140, 66, 20, 1, 96, 86] print("The list is :") print(my_list) occ_number = 66 print('{} has occurred {} times'.format(occ_number, count_num(my_list, occ_number)))
Output
The list is : [66, 26, 48, 140, 66, 20, 1, 96, 86] 66 has occurred 2 times
Explanation
A method named ‘count_number’ is defined, that takes a list and a number as parameter.
The list is iterated over, and if any element matches the number, the counter is incremented.
The counter is returned as result of the function.
Outside the function, a list is defined, and is displayed on the console.
The number is defined, and the method is called by passing these parameters.
The output is displayed on the console.
- Related Articles
- Find number of times every day occurs in a Year in Python
- Python Program to Find Element Occurring Odd Number of Times in a List
- Write a function that counts the number of times a given int occurs in a Linked List in C++
- Python program to find the largest number in a list
- Python program to find the smallest number in a list
- Python - Check if k occurs atleast n times in a list
- Python program to find largest number in a list
- 8086 program to search a number in a string
- How to count the number of times a value occurs in a column of an R data frame?
- Python program to find the second largest number in a list
- Program to find modulus of a number by concatenating n times in Python
- Program to count number of elements in a list that contains odd number of digits in Python
- Program to find the number of unique integers in a sorted list in Python
- Program to find the kth missing number from a list of elements in Python
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach

Advertisements