2

I'm sure this is simple and I'm looking right at it, but I cannot make it work.

Goal: Use a loop to go through the list of strings and find the search term. Return the element number of the first match.

I've tried a few options, nothing seems to work, and I have yet to find a working description of how to do it in any texts.

This is my best attempt so far:

def get_element_number(a_list, search_term):
    for i in range(len(a_list)):
      if search_term in a_list[i]:
        return a_list.index(i)
      elif not search_term in a_list:
        return 'no match'

Error message:

Traceback (most recent call last):
File "python", line 11, in <module>
File "python", line 5, in get_element_number
ValueError: 2 is not in list

Not looking for the complete answer, just any help in where I'm going wrong or if I'm missing something would be very helpful.

2
  • Can you provide a sample of a_list and search_term as made in the function call? Commented Jan 5, 2017 at 12:21
  • oh sorry! get_element_number(['a'', 'b', 'd'], 'd') I found a work around that it'll accept using the length of the list, but I definitely appreciate the help and real answers! Commented Jan 5, 2017 at 12:57

6 Answers 6

3

You can replace all of this with index = a_list.index(search_term).

Note that if the list doesn't contain the search_term, it will throw an exception, so you'll need to catch that and return "not found" or something similar. Second note: it only returns the first index of the found search_term.

Sign up to request clarification or add additional context in comments.

1 Comment

I figured out already what to do for the exception (basically broke down the challenge into parts, solved what I could and banged my head on the desk for a bit over the rest). Thank you for the help!
3

if search_term in a_list[i]: is True even if search_term is contained in a_list[i].

So in the case of an exact match index works, but in a case of a partial match index throws an exception.

Aside: elif not search_term in a_list: is wrong. Remove it or you'll return at first non-match.

Rewrite it as:

def get_element_number(a_list, search_term):
     try:
          return a_list.index(search_term)
     except IndexError:
          'no match'

This is simpler and has the advantage of performing search only once, which is important performance-wise when you're using linear search (not taking the overhead of exception into account).

Comments

0
for index, s in enumerate(a_list):
  if s == term:
    return index
return -1

Comments

0
def get_element_number(a_list, search_term):
    for index, value in enumerate(a_list):
        if search_term in value:
            return index
    return 'not match'

1 Comment

I was reading about enumerate, but I thought it worked a little different. Thank you!
0

Here's a simple change to your code that will resolve it:

def get_element_number(a_list, search_term):
    if search_term in a_list: #if search keyword is in the list
        return a_list.index(search_term) #then return the index of that
    else:
        return 'no match!' 

Comments

0
def find_element(a_list, search_term):
i = 0
for e in a_list:
    if e == search_term:
        return i
    i += 1
return -1

Very nice explanation for either this one or other solution you can find in the video linked below

https://www.youtube.com/watch?time_continue=216&v=GwOFHuPMCpM

2 Comments

"This is if you want to be consist in for loop." isn't a grammatically correct sentence -- can you clarify?
I meant "remain consistent" as he posted the issue using for loop to solve that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.