is there a way to do this with list comprehension? Thanks
Yes, comprehensions permit nested loops.
>>> def exponents(bases, powers):
return [b ** x for b in bases for x in powers]
>>> print(exponents([2, 3, 4], [1, 2, 3]))
[2, 4, 8, 3, 9, 27, 4, 16, 64]
>>>
Ahhhh thank you. I had â€śnew_lst = [base ** power for base in bases AND power in powers]â€ť
â€śandâ€ť instead of â€śforâ€ť
Also, didnâ€™t even occur to me that I didnâ€™t need a variable. Thanks for your help
Youâ€™re welcome. Now add to the challenge so that the result looks like,
[ [2, 4, 8], [3, 9, 27], [4, 16, 64] ]
Laterâ€¦
Have you given this any consideration? It might help to know that as we can write a comprehension on two lists (or dictionaries) and use nested loops to yield two values, we can also write a comprension on a comprehension that still yields two variables, but also lets us group the results according to one of the variables.
Just wanted to doublecheck that Iâ€™m understanding correctly. Here is my code for your reference:
def exponents(bases, powers):
return [[base ** power for power in powers] for base in bases]

Is it generally true that the result of the inner list comprehension
[base ** power for power in powers]
will be grouped according to the outer list comprehension (in this case, bybase
)? 
Is my understanding of whatâ€™s going on here sound?
For each base, a new list is returned where base has been raised to the power of each power in powers.
base = 2, power = 1
[[2]]
base = 2, power = 2
[[2, 4]]
base = 2, power = 3
[[2, 4, 8]]
base = 3, power = 1
[[2, 4, 8], [3]]
and so onâ€¦
Without grouping,
>>> def powers(bases, exponents):
return [a ** x for a in bases for x in exponents]
>>> powers([2, 3, 5, 7],[2, 3, 4, 5])
[4, 8, 16, 32, 9, 27, 81, 243, 25, 125, 625, 3125, 49, 343, 2401, 16807]
>>>
With grouping,
>>> def exponents(bases, powers):
return [[base ** power for power in powers] for base in bases]
>>> exponents([2, 3, 5, 7],[2, 3, 4, 5])
[[4, 8, 16, 32], [9, 27, 81, 243], [25, 125, 625, 3125], [49, 343, 2401, 16807]]
>>>
Man, youâ€™re a god, i find it so beautiful