Python Forum

Full Version: Error:unsupported operand type(s) for ** or pow(): 'list' and 'int'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
this code is generating the error message written on the subject line.
I am not understanding why.

L=[1,2]
L3=[3*L]
L4=[k**2 for k in L3]
print(L4)
did you print L3 to see what it is? :-)
yes,

[[1, 2, 1, 2, 1, 2]]

I am still unsure on why you asked me that.
(Mar-22-2019, 12:54 PM)mcgrim Wrote: [ -> ]yes,

[[1, 2, 1, 2, 1, 2]]

I am still unsure on why you asked me that.
don't you see that L3 is list of lists (i.e. there is actually just one element)
So, it the list comprehension value of k is [1, 2, 1, 2, 1, 2]
and you cannot raise a list to power 2

L=[1,2]
L3=[3*L]
print(L3)
for k in L3:
    print(k)
    print(k ** 2)