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
Find the error in the following list comprehension :
["good" if i < 3: else: "better" for i in range(6)]
Suggest corrections for the errors in both the previous questions.
Implement the following function for a linear list, which find outs and returns the number of unique elements in the list
def unique(lst): """passed parameter lst is a list of integers (could be empty)."""
After implementing the above function, test it with following lists and show the output produced by above function along with the reason for that output.
(i) lst = []
(ii) lst = [1, 2, 3]
(iii) lst = [1, 2, 2]
(iv) lst = [1, 2, 2, 3, 3]
Use a list comprehension to create a list, CB4. The comprehension should consist of the cubes of the numbers 1 through 10 only if the cube is evenly divisible by four. Finally, print that list to the console. Note that in this case, the cubed number should be evenly divisible by 4, not the original number.