[Solved] If func returns a list that I'll go over with a for loop and I won't be using the func again, which is preferred: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[Solved] If func returns a list that I'll go over with a for loop and I won't be using the func again, which is preferred:

That I assign the returned values to a variable and use for on that, or that I insert the func itself? A) Fn_list = Fn(x, y) for i in Fn_List: ... or B) for i in Fn(x, y) : ... It's A if I'm to call the function many times, right? Please tell me so if I am wrong on either or both cases. Thank you.

22nd Apr 2022, 3:21 PM
Korkunç el Gato
Korkunç el Gato - avatar
13 Answers
+ 2
If you need more instances to using Fn_list, then go for A Else B , no use of extra variable. You are right.
22nd Apr 2022, 3:24 PM
Jayakrishna 🇮🇳
+ 3
In first approach, you are calling function 4 times, each time a function is executing 10**6 instructions by loop.. 2nd approach, you are using just 2 call for function so its half instructions gets executed among .. time complexity is double for 1st way compare to 2nd.. Which one you choose.. I choose 2nd. In this, its clearly 2nd approach is far better..
22nd Apr 2022, 5:06 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 If you have time: Thank you. What if the list it returned were a humongous list and yet I wanted to call the function many times? Do I store that in another variable despite its length? Do I try to yield it ans use the alias as many times as I want?
22nd Apr 2022, 3:31 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Am not understand your query full.. Using to store in variables makes code clean and readable.. If it is less variable, no difference but if you need thousands variable better not to use each time. And when ever possible.. Homogenous or not, it's depends on your code, how you using it. If you can use yield then use it, instead of return, when ever possible.. If this not answered, then try to give a sample code snippet, I may understand then..
22nd Apr 2022, 3:41 PM
Jayakrishna 🇮🇳
+ 2
Yay, thanks, so storing big values isn't that much of a problem, and time comes first. Okay, thank you!
22nd Apr 2022, 5:08 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Yes. Execution speed important. And more space means more time to execute.
22nd Apr 2022, 5:21 PM
Jayakrishna 🇮🇳
+ 2
HungryTradie No. Calling with single argument or any number will not make any difference in Big O.. Reduces iteartions but same amount of operations..! How..? It calling function 2 times only instead of 4 so reduces operations also.. Python allows two return more than one value. Yes. Possible. Pass 4 variables and in loop, Use 2 lists: lst.append(i**2 - y)b / x) lst2.append(i**2 - y2)b / x2) return 2 lists. Python has lambda expression and functional support to pass functions as parameters... def func1(self, x, y, func2) : Use function func2 here.. n = func2(x, y) Hope I answered what you asked about..! Edit: The Time complexity I mean in reply is in general about time for number of instructions executed.. Not in the Big O notation sence.
23rd Apr 2022, 2:31 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Here's an example Jaya. It has no point except to show what I mean. You already answered my main question, I know, and I thank you for that. This is only to understand preferences further. I commented out the for loop beginning because the range is 1000000. https://code.sololearn.com/cN3FDxliPjdF/?ref=app
22nd Apr 2022, 4:51 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
G'day Jayakrishna🇮🇳 would the big O be reduced by calling the function once with all 4 variables, doing both algorithms in the same loop iteration? I see that it reduces the iterations of the loop by half, but has the same amount of operations (has to do 2 calculations per loop instead of 1). Or is there no way to have 2 lists as the return value of a function? Does python have a similar mechanism to pointers in C, so the function can write directly to a list outside of the function scope?
22nd Apr 2022, 10:27 PM
HungryTradie
HungryTradie - avatar
+ 1
HungryTradie I searched for Big O and there came up a new world. I think I should get more notebooks, and start data structures and algorithms. I am too limited in my knowledge to make most of the responses shared here. Thank you for expanding the question to something theoretical and showing what subject heading this lies under. Keywords: Time complexity, Big O
23rd Apr 2022, 10:30 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Sorry for my tick-upvote confusion again, and thanks for the clarification :-)
23rd Apr 2022, 2:35 PM
Korkunç el Gato
Korkunç el Gato - avatar
23rd Apr 2022, 9:27 PM
HungryTradie
HungryTradie - avatar
+ 1
HungryTradie, Thanks a lot for the link, you and Brian have provided great sources, I've read through Brian's (the examples used by the mentor were in Java, but most is still understandable along with the text) and I looked at SO link of yours and the cheat sheet. Cheat sheet seems great because I haven't studied data sorting algorithms yet, so rhis gives me perspective, as I might confuse DSA names. Thank you :-)
23rd Apr 2022, 10:33 PM
Korkunç el Gato
Korkunç el Gato - avatar