How to perform exponentiation with values in a nested list

I’m trying to perform exponentiation with values inside a nested list. I think my main issue is understanding how parameters work with nested lists and if they act as two separate entities or if I need to assign them to new variables.

Create a function named exponents() that takes two lists as parameters named bases and powers . Return a new list containing every number in bases raised to every number in powers .For example, consider the following code.

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

the result would be the list [2, 4, 8, 3, 9, 27, 4, 16, 64] . It would first add two to the first. Then two to the second. Then two to the third, and so on.

Here is my code. I feel like I’m close but my equation seems to cause the function to go out of range.

At it’s core I know I have to do

bases ** power

#Write your function here

def exponents(bases, powers):
  new_lst = []
  for base in range(0, len(bases)):
    for power in powers:
      #print(bases[base])
      #print(powers[power])
      value = bases[base] ** powers[power]
      new_lst.append(value)
    return new_lst
  return new_lst


#bases ** powers

#Uncomment the line below when your function is done
print(exponents([2, 3, 4], [1, 2, 3]))

I am very confused by the fact that you use two different styles of loops:

  for base in range(0, len(bases)):
    for power in powers:

the first/outer loop (bases) gives you the indexes given you use range
the second/inner loop (powers) gives you the values from the list directly.

so this:

bases[base]

make sense, given base variable contains index values thanks to range

powers[power] doesn’t make much sense, power already contains the value from the list. So why would you need to an index lookup?

range() is really useful if we need indexes to update elements in the list. Which we don’t need here, so don’t use range. Just get the values from the list, that really simplifies your code :slight_smile:

2 Likes

In addition to @stetim94’s great explanation, be careful with return statements. Remember that once a return statement is executed, the function is exited. Take a closer look at your code and make sure that you are iterating through every element in bases.

3 Likes

@stetim94 @dr_victoria

Thanks for the info on range. I was overcomplicating my code and was able to resolve this issue.

Also, returns confuse me, I think I’m getting better but they are tough to comprehend where you might put it in regards to your loop. I think I’m starting to understand it the more I use it however.

#Write your function here

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


#bases ** powers

#Uncomment the line below when your function is done
print(exponents([2, 3, 4], [1, 2, 3]))
2 Likes

return does literal what it says, returning/handing back data to the caller.

We can not access new_lst outside the function (scope rules), so being able to return the data to caller is really useful.

But just like when you make a test/assignment, when you hand the assignment back in (aka return), you don’t have to answer all the questions. Same here. A function can end earlier if you want

1 Like