0

I would like to know whether a function could return a loop?

My code is as follow:

def looping():
    a = [25,50,75,100, 200, 300]
    for x in a:
        return x
print(looping())

When I print it it returned:

Output:

[25]

Expected output:

25
50
75
100
200
300

Is this possible?

3
  • instead of return x you can just print(x) Commented Aug 28, 2018 at 5:33
  • 1
    yield instead of return (and list(looping())) Commented Aug 28, 2018 at 5:33
  • I am hoping to return each of the element for writing it into file name so i cant print it Commented Aug 28, 2018 at 5:35

3 Answers 3

6

First, the problem with your existing code is that once you return, the function is over. So, you just return the first value and you're done.


A function can't return a loop, because a loop isn't a kind of value.

A function can return an iterable, which is a value that can be looped over. There are a few ways to do this:


You could use yield, as in most of the other answers, to make your function a generator function, so it returns an iterator over all of the things you yield.

But yielding inside a loop is just a clumsy way of doing yield from, so really, you should just do that:

def looping():
    a = [25, 50, 75, 100, 200, 300]
    yield from a
for x in looping():
    print(x)

You can instead return an iterator directly:

def looping():
    a = [25, 50, 75, 100, 200, 300]
    return iter(a)
for x in looping():
    print(x)

Or, most simply of all: a sequence, like a list, is just as iterable as an iterator. So, if you have a list, just return it:

def looping():
    a = [25, 50, 75, 100, 200, 300]
    return a
for x in looping():
    print(x)

While we're at it, you don't even really need an explicit loop there. You can call a function that hides the loop inside, like join:

print('\n'.join(looping()))

Or you can even splat the iterable directly to print:

print(*looping(), sep='\n')

Although in this case, I think the loop is perfectly reasonable, so if you're happy with it, stick with it.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi sir, is there a way to use the function like this? with open("text_{}.txt".format(looping()),"w+") as outfile: outfile.write(something)...... I wanted it to automatically renamed according to my input within the list. Sometimes I have too many files and wanted to name it according to the number
@JJson No, but you could just do for name in looping(): with open("text_{}.txt".format(name), "w+") as outfile:.
@JJson I think what you’re looking for is array-oriented computation, like in APL or J. Python does provide some support for that, through third-party libraries like NumPy, but it’s mainly designed around a different paradigm, iterators: instead of hiding loops, it makes loops as powerful and ubiquitous as possible.
2

If you merely want to output the contents of a list, you may use the .join() feature.

print("\n".join([str(x) for x in a]))

If you want to utilize loops, use a generator, and write the loop outside as shown.

def looping():
    a = [25, 50, 75, 100, 200, 300]
    for x in a:
        yield x

for x in looping():
    print(x)

Comments

1

It can be done without a loop using just join and map :

>>> print('\n'.join(map(str,a)))

But, if it is a loop that you are looking for, then using return is the wrong way to go about it, as return would give back the control from the function in its first encounter itself.

Instead, use yield or yield from :

yield from iterable is essentially just a shortened form of for item in iterable: yield item

>>> def looping(): 
        a = [25,50,75,100, 200, 300] 
        yield from a

>>> for ele in looping(): 
        print(ele)

#driver values :

IN : a = [25,50,75,100, 200, 300]
OUT: 25
     50
     75
     100
     200
     300

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.