recursive linear search python

"""
This is rather straight forward:
The function just needs to look at a supplied element, and, if that element isn't the one we're looking for, call itself for the next element.
"""

def recrsv_lin_search (target, lst, index=0):
    if index >= len(lst):
        return False
    if lst[index] == target:
        return index
    else:
        return recrsv_lin_search(target, lst, index+1)

#test it
example = [1,6,1,8,52,74,12,85,14]
#check if 1 is in the list (should return 0, the index of the first occurrence
print (recrsv_lin_search(1,example))
#check if 8 is in the list (should return 3, the index of the first and only occurrence)
print (recrsv_lin_search(8,example))
#check if 14 is in the list (should return 8, the index of the last occurrence)
print (recrsv_lin_search(14,example))
#check if 53 is in the list (should return False)
print (recrsv_lin_search(53,example))

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source