Python functions + lists

Asked

Viewed 123 times

2

When I try to create the function below I always get the first list value, it seems that it is not iterated. Someone could explain me why?

Follows the code:

novalista = []

def find_it(seq):
    number_list = seq
    for number in number_list:
        novalista.append(number)
        return novalista

print(find_it([2, 2, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5]))

novalista != number_list (Why?)

The print shows this:

[2]

2 answers

3


Simply why the return function within the for, if you put it out it will return the full list.

novalista = []

def find_it(seq):
    number_list = seq
    for number in number_list:
        novalista.append(number)
    return novalista

print(find_it([2, 2, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5]))

3

The main problem that contributes to the behavior you mentioned is that your indentation is wrong and, as a result, the return is inside your loop loop. Thus, it will finish the function already in the first loop iteration.

But it’s not limited to that, there are other problems in the code that will generate side effects that maybe you haven’t even noticed yet.

novalista = []

def find_it(seq):
    number_list = seq
    for number in number_list:
        novalista.append(number)
    return novalista  # Indentação corrigida

print(find_it([2, 2, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5]))

Another problem is that you modify the list novalista which has been defined outside its function in the overall scope (apparently). As a consequence of this, you will not be able to call the function again, because there will be "garbage" in the return of it.

>>> fint_id([1, 2, 3])
[1, 2, 3]

>>> find_it([4, 5, 6])
[1, 2, 3, 4, 5, 6]

Note that the return of your first call remained generating an unexpected output in the second execution of the function.

To solve, the ideal is to modify a locally defined object:

def find_it(seq):
    novalista = []
    number_list = seq
    for number in number_list:
        novalista.append(number)
    return novalista  # Indentação corrigida

Other points worth mentioning are:

  1. You call the function find_it that basically returns a copy of a list; it didn’t make much sense (name "search for" that returns a copy of the list without doing a search).
  2. Within the function did number_list = seq, this also there are no justifications to do, you just set a new name for your original list just to iterate it directly seq producing the same result.
  3. Now you name your structures in English, or in Portuguese, it would be interesting to maintain a standard.
  4. Now you name your structures using the underline as a separator, now puts all words together, it would also be interesting to maintain a standard (here, as convention defined in PEP 8, in Python it is usually separated with the _).

If the intention is really to make a copy of the list, there are other better and simpler ways to do:

nova_lista = lista.copy()
  • Great answer Anderson, thank you very much!

  • Thank you very much! It’s actually mixed English and Portuguese and a lot of things don’t make sense because this wasn’t the purpose of the code, I did it just to demonstrate a problem I was having with another ahahah program. Thank you so much for the feedback! Your tips for beginners like me are also very precious, hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.