Python Forum
iteration stops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: iteration stops (/thread-29390.html)



iteration stops - d8a988 - Aug-31-2020

quick question:

the following loop
list_nums= [i for i in range(15)]

    for i in range(len(list_nums)):
        if list_nums[i]<9:
            print(list_nums[i])
works as it should since all the numbers from 0 to 8 are printed,
but if I place the same loop into a function:
list_nums= [i for i in range(15)]
def fn():
    for i in range(len(list_nums)):
        if list_nums[i]<9:
            return(list_nums[i])
print(fn())
Only the first number is printed.
As far as I am understanding, the 'return' command interrupts the loop.

How do I make this function work the same way as the loop in the first code?
I am trying not to use any 'print' command inside the function.


RE: iteration stops - perfringo - Aug-31-2020

As I see it there are several problems.

- this is not phytonic way. Don't use indices, len etc. Canonical way in Python is to iterate directly over items:

>>> list_ = [1, 3, 5, 9]
>>> for item in list_:
...     if item < 5:
...         print(item)
...
- this function is not reusable as there are no arguments (list_nums is hardcoded)
- there are several ways to achieve desired result (as I understand this is printing elements which are lower than 9):

print(*[item for item in list_nums if item < 9], sep=', '])
--> 0, 1, 2, 3, 4, 5, 6, 7, 8

print(*filter(lambda item: item < 9, list_nums), sep=', ')
--> 0, 1, 2, 3, 4, 5, 6, 7, 8
If needed, separator can be set to newline ('\n').


RE: iteration stops - d8a988 - Aug-31-2020

Thanks for replying.
However, unfortunately my question has not been answered.
I just need to know how to change my function to make it work like the first for loop I wrote.
I know that there are no arguments, but I just wrote it this way to understand the mechanism.
If there is a way to change the function to make it print those numbers, I'd be happy to learn.


RE: iteration stops - jefsummers - Sep-01-2020

ok, this should work. See comments

list_nums= [i for i in range(15)] #your line
def fn(ln): #ln will be list_nums but less confusing scope
    output = [] #because you want all the numbers at once
    for i in ln: #proper for loop. Anything with range(len(anything)) there is a better way
        if i<9: 
            output.append(i) #add to list
    return output #this is what you return, not a single value
print(fn(list_nums))



RE: iteration stops - perfringo - Sep-01-2020

The 'mechanism' of the Python function is relatively simple: if function returns something, control returns to the function caller. This means that after return function is done. So as in jefsummers example - one must construct whole list first and then return said list at once.

That said one can make generator function and yield instead of returning. This enable yielding one element at the time and doesn't require building list:

>>> nums = [1, 3, 4, 5, 7, 8]
>>> def evens(list_):
...     for item in list_:
...         if item % 2 == 0:
...             yield item
...
>>> print(*evens(nums), sep='\n')
4
8