Create a function that takes a list and a number as arguments and returns the index of the number in the list if it exists. Otherwise, it should return -1.

Example 1:

Input: [1, 3, 5, 7, 9], 5 
Output: 2

Example 2:

Input: [2, 4, 6, 8, 10], 7 
Output: -1

Use the built-in index() method to find the index of an element in a list. Handle exceptions when the element is not found in the list.

def find_index(lst, n):
return lst.index(n)
except ValueError:
return -1

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!