Function with loop repetition in python

-1

Hello,

I am trying to use a function to get the complete list of containers regardless of status, "docker ps -a". When I use the code outside the function, it works.

ButwhenIusethesamecodeinsidethefunction,itonlyreturnsmethefirstelementinthelist.

Why does this happen?

    
asked by anonymous 02.11.2018 / 05:11

1 answer

0

This happens because a return is always going to be the end of a function.

You can have infinites return , the first one that touches your function will be terminated and will return the object that the return is pointing to.

Example:

Function that prints a list.

In both cases they will receive as input:

[1,2,3,4]

Below are two cases, the latter being what you are using.

def printar(n):
    for x in n:
        print(x)

This would be a perfect skeleton for your code. The result would be:

1
2
3
4

However, what you are trying to do is:

def printar(n):
    for x in n:
        return(x)

That the result would be:

1
    
02.11.2018 / 05:24