-3

So I want to make a polynomial equation solver but .index(), the function that retrieves the index of an element, only retrieves the first occurrence or so I was told. How can I fix this without any direct references?

  • 2
    Have you tried *anything* at all? Maybe iterate through the list, and stop at the second instance of the word you are looking for? – juanpa.arrivillaga Apr 12 '21 at 18:37
  • 1
    Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. Invest some time with [the Tutorial](https://docs.python.org/3/tutorial/index.html) practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. – wwii Apr 12 '21 at 18:37
  • Does [How to find all occurrences of an element in a list](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) answer your question? – wwii Apr 12 '21 at 18:39

1 Answers1

0
items = ['hey', 'farts', 'hey', 'no']

def findElement(items, search_element):
    counter = 0
    for ind, item in enumerate(items):
        if(item == search_element):
            counter+=1
        if(counter == 2):
            return ind
    return -1


print(findElement(items, 'hey'))

Or this:

findElement = lambda items,search_element : [(ind, item) for ind, item in enumerate(items) if item == search_element][1][0]
print(findElement(items, 'hey'))
ssnk001
  • 170
  • 5