1
list_1  =[1, 2, 3, 4 ]

def fun(list_1):
    for each value in list1:
        # perform some operation and create a new data frame(pandas) for each value in the list
        # so in total I should get 4 data frames.
        print each_value

        return new_data_frame

When I run fun(list_1) I should get 4 data frames:

1
data_frame_1

2
data_frame_2

3
data_frame_3

4
data_frame_4

but I am getting an ouput only with first value.

1
data_frame_1

so what should I change in my function.

Elizabeth Susan Joseph
  • 6,255
  • 7
  • 20
  • 23
  • 1
    There is a `yield` keyword! http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python – cel Mar 10 '15 at 08:51
  • Sorry you want 4 dataframes but where are these going to be stored? In the callee or the caller? it's very unclear what you are trying to achieve here – EdChum Mar 10 '15 at 08:55
  • @EdChum - sorry I am new to programming. I dont understand the differene between callee and caller. what detail you need. I will provide it – Elizabeth Susan Joseph Mar 10 '15 at 08:58
  • Well as others have pointed out you return immediately from your function so this implies you want the caller i.e. the external caller to store the result but you then state you don't want a list of dfs so where are these dfs going to be stored and used? – EdChum Mar 10 '15 at 09:01
  • 1
    @ElizabethSusanJoseph The callee is the one who is called, the caller is the one who calls. – glglgl Mar 10 '15 at 09:01
  • @EdChum when I call the fun(list1) function it should just return all these data frame value. Yes I have moved my return statement just one tab before as suggested by Akis. – Elizabeth Susan Joseph Mar 10 '15 at 09:04
  • 1
    but you then need to store these dfs somewhere which means either in a list or tuple or some other container or use a generator – EdChum Mar 10 '15 at 09:06
  • @EdChum - it is a stupidest question. I deserve negative points :) . ok now I got the concept. I thought generator is just equal to return and it makes no difference. – Elizabeth Susan Joseph Mar 10 '15 at 09:09
  • @EdChum no you **don't need** to store them they could be generated on the fly with `yield`. And no `yield` is not *just equal* to `return` even though they seem to do the same job *in the end*, but under the hood and how to use them is completely different. See the link I added at the end of my answer to get a better understanding of `yield` ;) – d6bels Mar 10 '15 at 09:12
  • @d6bels Yes I got the concept of yield. I have implemented it in by solution. – Elizabeth Susan Joseph Mar 10 '15 at 09:16

1 Answers1

5

When you return from the function, it's over.
You're out of the function and that's it.

You could use a generator to achieve what you want or you may return a tuple or a list.


Using a generator :

def fun(list_1):
    for each_value in list_1:
        # perform some operation and create a new data frame(pandas) named "new_data_frame" for each value in the list
        print each_value
        yield new_data_frame

for data_frame in fun(list_1):
    # Do something with the data frame "data_frame"

This will print the value each time, and return your data frame. But it returns a generator, so you have to call the function multiple times. What you cannot really do is get your data frames in a loop without wanting to store them in an list or assimilated or call the function only once.


A simpler way with a list could be:

def fun(list_1):
    data_frames = []
    for each_value in list1:
        # perform some operation and create a new data frame(pandas) named "new_data_frame" for each value in the list
        print each_value
        data_frames.append(new_data_frame)

return data_frames

For reference, I'd suggest you have a look at What does the "yield" keyword do in Python? which may be interesting for you.

Community
  • 1
  • 1
d6bels
  • 1,432
  • 2
  • 18
  • 30