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.

Somebody, please clear my doubts.
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