list comprehension question

Jim Jinkins j-jinkins at usa.net
Sun Aug 11 17:18:28 EDT 2002


brobbins333 at shaw.ca wrote:

>Why doesn't this work?
>
>list = [range(2, 12)]
>[(x ** 2) + x + 2 for x in list]
>
>error: unsupported operand type for **
>  
>
Try lst = range(2, 12) instead of [range(2, 12)]

In your example the first/only element of list is [2, 3, ...., 11], 
which is not a number and cannot be squared.

BTW list as a variable name shadows  the builtin type name, list.  It 
would be better to use lst or something like that.

    Jim




More information about the Python-list mailing list