Is there a bug in the exercice?

hi,

my code from this exercise works, but from some reason is rejected as an answer:

list_1 = [2, 2, 2, 1, 2, 1, 0, 0, 0, 2]
list_2 = [2, 2, 2, 1, 2, 1, 0, 0, 0, 2]


def find_first(list_x, element):
    
    
    
    for i in list_x:
        index = 0
        if i == element:
            index = list_x.index(i)
            return index
        if element not in list_x:
            return -1
            
    
print('list_1 type =', type(find_first(list_1, 1)),"list_1 =", find_first(list_1, 1))
print('\n')
print('list_2 type =', type(find_first(list_2, 99)), 'list_2 =', find_first(list_2, 99))

rejected output:

@drill_n_bass:

This works:

def find_first(list_x, element):
    for i, value in enumerate(list_x):
        index = 0
        if value == element:
            index = list_x.index(value)
            return index
    return -1

With this your function returns -1 immediately it doesn’t see the value. It doesn’t look through all the values first.

Enumerate is needed here so that you can get both value in the list, and its index at once.

You can do without enumerate here because we use the index function to get the index. Without the index function, I will just set index to i: index = i.

Hi @drill_n_bass,

The issue with your code is only the indentation of the second if-statement:

def find_first(list_x, element):      
    for i in list_x:
        index = 0
        if i == element:
            index = list_x.index(i)
            return index
    if element not in list_x:
        return -1

For the rest, it looks ok.

1 Like

Hello @drill_n_bass,

Suggestions from both @monorienaghogho and @Elena_Kosourova looks good.
Also, be sure to read the following tweaked code for variable index initialization and the default return -1 statement:

list_1 = [2, 2, 2, 1, 2, 1, 0, 0, 0, 2]
list_2 = [2, 2, 2, 1, 2, 1, 0, 0, 0, 2]


def find_first(list_x, element):
    index = 0  # variable index should be initialized outside the for loop. If inside, it keeps resetting.

    for i in list_x:
        if i == element:
            index = list_x.index(i)
            return index  # If element is found, function halts here.

    return -1  # If element isn't found, it defaults to -1. No need for an exclusive `not in` check.
               # This should be outside the for loop.



def find_first_with_enumerate(list_x, element):
    for i, e in enumerate(list_x):  # enumerate returns both the index(i) and the element(e) on list_x
        if element == e:
            return i
    return -1

print(find_first_with_enumerate(list_1, 1))
print(find_first(list_1, 1))

Hope this clarifies.

1 Like

in what way? isn’t it the same because of “for” loop ? Each time when " i == element " the value of “index” will be updated anyway…

this is ok, becuse the task was quote: “Returns the index of the first occurrence(…)”

Yep, then, I missed this one, It caused rejection of my answer.

I will learn tonight about this method. Thank you for a practical example of usage! :smiley:

in what way? isn’t it the same because of “for” loop ? Each time when " i == element " the value of “index” will be updated anyway…


True, it doesn’t make a difference it’s just an overhead to keep setting it to 0 for every for-run.
Initializing once is enough.

1 Like

just checked. You were right.

In my version it tooks 108 steps.

When I put index away from loop, just 84 steps. Now I see the logic of this placement. Thank you :slight_smile:

1 Like