FAQ: Linear Search: Python - Implement Linear Search

This community-built FAQ covers the “Implement Linear Search” exercise from the lesson “Linear Search: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Search Algorithms

FAQs on the exercise Implement Linear Search

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

What’s the purpose of printing the first number, 54? What an awkward code.

2 Likes

Hello! I was just confused on why you cannot add another conditional statement with this algorithm

Here is my code:

def linear_search(search_list, target_value):
  for idx in range(len(search_list)):
    print(search_list[idx])
    if search_list[idx] == target_value:
      return idx
    else:
      raise ValueError("{0} not in list".format(target_value))

The last part of the solution works like this:

raise ValueError("{0} not in list".format(target_value))

I don’t mind but why can I not use an additional else statement in this case? Thank you in advance.

2 Likes

I did the same as you so I have the same question :nerd_face: :face_with_monocle:

1 Like

Me too!!! I don’t really get the question!

def linear_search(search_list, target_value):
for idx in range(len(search_list)):
print(search_list[idx])
if search_list[i] == target_value:
return idx

because the raise error is inside the for loop, he will only look the first element “search_list[idx]” if that is not the target_value it will raise a error right after, you need to raise an error only after the for loop ends, which means the value was not found inside the list

1 Like

I am currently on searching arrays in the python master the technical interview.

def linear_search(search_list, target_value): for i in search_list: if i == target_value: return search_list.index(i) else: continue

I am a bit confused on why they didn’t just use this snippet of code.
Its more easy to understand than the example given.

That doesn’t raise a ValueError if the target_value is not found.
You don’t need else because if the value is found then it would return the index and never raise a ValueError. If you don’t want an ValueError then this is not necessary:

else:
  continue

because the the loop would iterate again without continue.

I think that is kind of incorrect; that function would only return idx if the first element in search_list is idx, it would raise an error otherwise which stops the program before the program even checks the second element. target_value might be positioned at the second element on the list and the program would raise this:
ValueError("{0} not in list".format(target_value))
before it checks the second element which is the target_value.
Does that information help?

Thanks a lot! great reply. However, how does python know that after the loop it has to raise a value error only if “none” was the output of the for loop i.e. no number found in the loop? Is this specified within the “value error function” that if the output of anything before was “None”, it should activate the raise value error and if the output before was not “None” (i.e. number was found) it should ignore the “raise Value Error”?

Thanks a lot

1 Like

It raises a ValueError if the element is not in the list. It will already have exited the function if it is found. If it hasn’t exited the function after the loop it will raise a ValueError since the target value is not found.

So the problem with this else statement is that if the first value is not the value you are looking for it will raise an error message. It needs to be outside the for loop so it executes once the code has finished iterating through the list.

So to help you with this one, it seems that when you use return in Python that effectively exits out of the function, so because you are using return on the index it would never actually reach the ValueError code unless the value isn’t found.

Of course I could be wrong, just a pattern I have noticed that if you use return it will only print one index when it comes to iterating through lists that being the first relevant one to the code in question).