CBSE Class 12 Computer Science
Question 64 of 68
Data Structures - I : Linear Lists — Question 1
Back to all questions 1
Question Write a program that uses a function called find_in_list() to check for the position of the first occurrence of v in the list passed as parameter (lst) or -1 if not found. The header for the function is given below :
def find_in_list(lst, v):
""" lst - a list
v - a value that may or
may not be in the list """
def find_in_list(lst, v):
if v in lst:
return lst.index(v)
else:
return -1
lst = eval(input("Enter a list : "))
v = int(input("Enter a value to be checked: "))
print(find_in_list(lst, v))Enter a list : [1, 2, 4, 7, 2, 55, 78]
Enter a value to be checked: 2
1
Enter a list : [10, 30, 54, 58, 22]
Enter a value to be checked: 22
4
Enter a list : [9, 5, 3, 33]
Enter a value to be checked: 10
-1