Nested Loop Confusion

Hi guys, i did solve these two problems myself without googling.
I have little confusion on nested loops here:
1st code:

``````def exponents(bases, powers):
new_list = []
for i in range(len(bases)):
for j in range(len(powers)):
new_list.append(bases[i] ** powers[j])
return new_list

print(exponents([2,3,4],[1,2,3]))
``````

2nd code:

``````def larger_sum(lst1, lst2):
sum1 = 0
sum2 = 0
for i in range(len(lst1)):
sum1 += lst1[i]
for j in range(len(lst2)):
sum2 += lst2[j]
if sum1 > sum2:
return lst1
elif sum1 == sum2:
return lst1
else:
return lst2

print(larger_sum([1, 9, 5], [2, 3, 7]))

``````

In the second code, i was not getting the solution due to using of nested loops, I have little doubts that, in the second code too, there are two parameter lists. But , why donâ€™t we use nested loop here, while in the 1st code, we used nested loop as I need to multiply the base index to the power index.

I will be very thankful to you.

Thanks
New coder

Itâ€™s a bit of an odd question and Iâ€™m you could go about it but with a slight apology for answering a question with a question, why would you even want to?

Wherver possible itâ€™s probably best to avoid loops entirely in Python, let alone nested loops. So for a basic answer Iâ€™d go for we donâ€™t use nested loops because we donâ€™t have to.

What kind of reply is that?
How my question is odd?
I am a newbie here, so i just want to clear my doubts.

I found it odd because you seem to want to use nested loops (a more compliacted solution) for a problem you say youâ€™ve already solved. Why make it harder for yourself and for anyone who reads you code?

The purpose of the first function is different to that of the second. I assume you used two loops because it seemed like the easiest way of solving the problem (this is assuming you actually wanted each item in the first list raised to every power in the second list; providing nine values in total for the given example). So weâ€™re back to the question of why youâ€™d ever want to try using them on your second problem.

Itâ€™s odd because in order to wonder why it shouldnâ€™t be nested, youâ€™d need an argument for why it would be nested

And itâ€™s odd because, have you thought about what should happen? Is that something that is nested?

Maybe if you write down what should happen in plain human language you could then look at that and ask yourself whether that strategy is a nested loop.

1 Like

Hello @thiyamsureshsingh.
It simply depends on the situation, for your first project it made the code simpler, but as you found out, it wouldnâ€™t work in the second.

There is not always a specific reason for why something doesnâ€™t work, it just doesnâ€™t.
Take for example, a guy trying to unlock a door with a thin piece of string.
Under normal circumstances, he will probably fail and eventually give up. Though he could easily get in with a lock pick, or his key.

As you practice, you will continue to learn how to use your â€śtoolsâ€ť, and when they can and should be used.

You were correct here that it wonâ€™t work well, but there isnâ€™t much of a detailed reason for why it wonâ€™t work.

Sometimes the best way to find out is to experiment, other times if you get stuck you can ask for help or you can look at the documentation for the language you are using.

1 Like