KnowledgeBoat Logo

Computer Science

Write a program that uses a function called findinlist() 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 """

Python

Linear Lists

1 Like

Answer

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))

Output

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

Answered By

2 Likes


Related Questions