0

Is it possible to make a two functions with seperate for-loops but that the second function gets what the first one did? I have a very simple example:

a = [1,2,3,4]

def func():
    for i in a:
        if i == 1:
            continue

def func2():
        func()
        for i in a:
            print(i)

print(func2())

The problem with the second function is that it doesnt "get" what the first function does. How could I fix this? I want the second function to get the first one so that I can continue on with statements.

4
  • can you save the results of the first loop into a list? Commented Nov 30, 2020 at 22:07
  • What do you mean by 'get'? Commented Nov 30, 2020 at 22:08
  • This question is a bit unclear, at least to me. Can you explain what output you're trying to get? Commented Nov 30, 2020 at 22:08
  • What exactly did the first function do that you expect to be visible to the caller? Commented Nov 30, 2020 at 22:09

1 Answer 1

2

To pass information from one function to another, the first function needs to return it.

def func(a):
    r = []
    for i in a:
        if i == 1:
            continue
        r.append(i)
    return r

def func2(a):
    r = func(a)
    for i in r:
        print(i)

func2([1, 2, 3, 4])
Sign up to request clarification or add additional context in comments.

1 Comment

FWIW, you can rewrite func as a comprehension: return [i for i in a if i != 1]

Your Answer

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