Programming Problems · Q1
Q.Write a program to find the number of times an element occurs in the list.
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
57% · 17/30 Questions
✓ Free question
Python lists already know how to count themselves: list.count(x) returns how many times
x appears. We read the list and the element from the user, then one method call gives the answer —
no manual loop needed (though a loop version is shown too, since that is what count() does inside).
The idea
Counting occurrences means scanning the list once and adding 1 every time an element equals the
target. The built-in method count() does exactly this scan for us.
Program
# Count how many times an element occurs in a list
n = int(input("How many elements? "))
lst = []
for i in range(n):
lst.append(int(input("Enter element " + str(i + 1) + ": ")))
target = int(input("Element to count: "))
print(target, "occurs", lst.count(target), "time(s) in", lst)
Sample run
How many elements? 6
Enter element 1: 4
Enter element 2: 7
Enter element 3: 4
Enter element 4: 9
Enter element 5: 4
Enter element 6: 7
Element to count: 4
4 occurs 3 time(s) in [4, 7, 4, 9, 4, 7]
What count() is doing (manual version)
count = 0
for item in lst:
if item == target:
count += 1
print(count) # 3 for the run above
Key lines explained:
lst.append(...)grows the list one input at a time — the standard way to read a list of n values.lst.count(target)walks the whole list and returns the number of elements equal totarget; it returns0when the element is absent (no error).- The manual loop shows the same logic: initialise a counter to 0, add 1 on every match.
✓Final answer
Read the list, then use lst.count(target) — e.g. for [4, 7, 4, 9, 4, 7] and target 4
the program prints 4 occurs 3 time(s). count() returns 0 if the element is not present.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.