Write a program to find the number of times an element
occurs in the list
Answers
Following are the code in python for the above question :
Explanation:
lis=[1,2,3,4,5,6,7,8,9,0] #default list which can be changed.
count=0 #variable to count the occurence of the elemnt.
element= int(input("Enter the elemnt to serach in the list")) # take the input from the user.
for x in lis: #for loop to search the element.
if(element==lis[x]):
count=count+1
if(count>=1): # check the condition.
print("The number "+str(element)+" is present in the list are "+str(count)+" times")
else:
print("The number "+str(element)+" is not present in the list")
Output :
- If the user inputs is 4, then it will prints 1 times.
- If the user inputs is 14, then it will prints items is not found.
Code Explanation:
- The above code is in python language, which has one default list which can be changed by the user.
- Then the input is take from the user for the element.
- Then for loop is used to check the element from the list.
Learn More:
- Python program : https://brainly.in/question/5558161
The following codes are written in Python.
Source code:
l = eval(input("Enter your list: "))
x = eval(input("Enter the element you want to count: "))
if x in l:
print(x, "appears in the list", l.count(x), "times.")
count() is a function that counts the number of times an element occurs in a list.
eval() was used to obtain the character, as we aren't sure if the list is purely integers, strings or a mix. eval() enables the data to be processed accordingly.