Why does my list get longer everytime its run?

Hi everybody,

I tried that but I have a wrong answer:
< 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]/>

with these lines of code:

<>
new_lst =
def exponents(bases, powers):
for num in bases:
for exp in powers:
new_lst.append(num ** exp)
return new_lst
</>

return new_lst is same indentation than for num in bases

How do you use </> you put “p” inside or something else?

Thx

you are missing a “/” on the top one.

for num in bases it will first go through 2, then 3 then 4
for exp in powers it will then go to 1, 2, 3 for each of the above.

for num in bases it will first see 2.
for exp in powers it will loop through each one of the values in the list [1, 2, 3].
2 ** 1
2** 2
2** 3

After that it goes back up to for num in bases and then go to 3 then goes down to for exp in powers and does the same thing it will loop through the list [1, 2, 3]

3 ** 1
3 ** 2
3 ** 3

This is why you are getting each number in bases to each number in exp, in the way you are writing the code.

If you want the 0 index in bases to ** with 0 index of powers, then I would use indexes. I’m sure this is not the only way to do it, but that’s my suggestion.

Be sure to define the new list inside the function else it doesn’t get reset with each call made, but keeps growing.

def func(foo, bar):
    result = []

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.

4 Likes

Hi Roy @mtf can you help me please, I don’t know why my code is wrong ( the result it’s correct but it still show me that is not okay, but I don’t understand why…
This was my code:
**new_list =
def exponents(bases,powers):
for x in bases:
for y in powers:
new_list.append(x ** y)
return new_list
**
this is the error [ 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]
, but the terminal returns the correct answers :thinking: why?

It has to do with the list object being created outside of the function. It should be initialized inside so it starts out empty every time the function is called.

def foo(a, b):
    bar = []
1 Like

Thank you @mtf it works perfectly! :clap:

1 Like