Python Forum
Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' (/thread-16969.html)



Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' - mcgrim - Mar-22-2019

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)



RE: Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' - buran - Mar-22-2019

did you print L3 to see what it is? :-)


RE: Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' - mcgrim - Mar-22-2019

yes,

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

I am still unsure on why you asked me that.


RE: Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' - buran - Mar-22-2019

(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)