Q. 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 (Ist) or-1 if not found. The header for the function is given below:

 

def find_in_set (lst, v ):

""" lst - a list

v - a value that may or may not be in the list """


Answer :-

def find_in_set (lst, v ):
      if v in lst :
            return lst.index( v )
      else :
            return -1
    
lst = eval(input("Enter a list :-"))
value = int(input("Enter the number which you want to search :-"))

print(find_in_set (lst, value ))

Output:-

Enter a list :-[1,2,3,4,5,6,7,8,9,10]
Enter the number which you want to search :-8
7
>>>

Enter a list :-[11,12,13,14,15,16,17,18,19]
Enter the number which you want to search :-21
-1
>>>

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post