list comprehension question

Jim Meier jim at dsdd.org
Sun Aug 11 16:09:22 EDT 2002


On Sun, 11 Aug 2002 13:53:21 -0600, brobbins333 wrote:

> Why doesn't this work?
> 
> list = [range(2, 12)]
         ^^^^^^^^^^^^^^
This line asks the range function to create a list of numbers, and then
put that list inside a second list. It ends up looking list this:
[[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]

This is a list with one item in it. That one item is a list object with
other items in it.

What you probably meant is:
list = range(2,12)

Which would make the list [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


> [(x ** 2) + x + 2 for x in list]
> 
> error: unsupported operand type for **

In your original version, the only x to be found in list would be the
interior list [1,2,3...], and indeed, ** is not defined for lists.

-Jim



More information about the Python-list mailing list