# FAQ: Code Challenge: Loops - Exponents

Ah, got ya. My original solution was:

``````def exponents(bases, powers):
new_lst = []
for b in range(len(bases)):
for p in range(len(powers)):
result = bases[b] ** powers[p]
new_lst.append(result)
return new_lst
``````

However, I can see that some of the code is redundant:

``````def exponents(bases, powers):
new_lst = []
for b in bases:
for p in powers:
result = b ** p
new_lst.append(result)
return new_lst
``````

Cheers!

Or given to using the index for read-only purposes. We only need to use this functionality if the intent is to be able to modify the value of list items. No harm in writing code that way, as long as we see what it is doing… Giving us write permission.

The technical difference is that one, `in range()` uses the members in a range sequence as indices for accessing values; whereas, the other `in sequence` uses the members of the sequence as read-only values taken directly from the list.

There is also a difference in which methods are used in the background but I’m not the best at describing that much detail so will defer to better information sources than the generalized picture we can give here.

From a practical view, if all we wish our function to do is read the sequences we give them, the `in sequence` is the way to code that particular function.

Whatever your decision, do not code one way or another because someone told you it is better. Write however you wish and discover for one’s self what might be more suitable to a given situation. In other words, and I hate to say this, don’t listen to the don’ters unless you clearly understand and agree with what they’re saying not to do. Otherwise ignore and keep on coding as you would given whatever the prevailing circumstances.

Right. The truth is that I did not actually know why the suggestion made by @patrickd314 was a superior one, other than his code being shorter and therefore supposedly more efficient.

It is becoming clear to me that one can do something well without actually understanding what it is that he is doing, at a core level. I have heard a similar sentiment towards those who graduate a 4 month intensive coding boot-camp, when compared to CS graduates who have gotten to grips with the intensive maths that is fundamental to software engineering.

1 Like

Hey, could someone help me understand why im getting an error with my code on this question??

Heres my result

``````result = []
def exponents(bases, powers):
for b in bases:
for p in powers:
result.append(b ** p)
return result
``````

when i look at the right side of the screen, the result that is printed is exactly correct! yet the module is telling me im wrong…
`exponents([2, 3, 4], [1, 2, 3])` should have returned `[2, 4, 8, 3, 9, 27, 4, 16, 64]` , and it returned [2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]

what i dont understand is why the correct answer is printed, i had the exact same issue on another question…
thanks!

``````>>> result = []
>>> def exponents(bases, powers):
for a in bases:
for x in powers:
result.append(a ** x)
return result

>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64]
>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]
>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]
>>>
``````

The result list needs to be initialized inside the function, else it never gets reset.

1 Like

Hi, when I practiced this task, this was what I wrote:

def exponents(bases, powers):
new_lst =
for base in bases:
power = [base**power for power in powers]
new_lst.append(power)
return new_lst

it results in three lists:
[[2, 4, 8], [3, 9, 27], [4, 16, 64]]

[2, 4, 8, 3, 9, 27, 4, 16, 64]
which is just one list, combining together

def exponents(bases, powers):
new_lst =
for base in bases:
power = [base**power for power in powers]
new_lst.append(power)
return new_lst

I still can’t figure out why make mine and the result of the standard answer code different.
If anyone can tell me why I would be grateful!

That is creating a separate list which ends up being appended to the new list, as a list

``````[ [...], [...], [...] ]
``````

when it should be a single, flattened list in the return.

``````[ ... ]
``````
``````    for base in bases:
for power in powers:
new_lst.append(base ** power])
``````

Since you are endeavoring to implement list comprehension what happens if we nest both in a single list?

``````>>> def exponents(bases, powers):
return [a ** x for a in bases for x in powers]

>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64]
>>>
``````
1 Like

Thank you so much mtf.
Your code is amazingly beautiful and concise.

2 Likes