list comprehension question

Tim Peters tim.one at comcast.net
Mon Mar 25 00:25:15 EST 2002


[Tripp Scott]
> thanks for the tip. actually, the essence of what i wanted to
> ask was: "can that SOMETHING be a list of more than one elements
> which will be _flatly_ added to the result list."

No.  len([f(x) for x in y]) == len(y) whenever no exception occurs,
regardless of the form of f() or type of y.

> as another example, can i generate this list
>
>   [1,1  2,2,2,  3,3,3,3, 4,4]
>
> with a list comprehension that contains one 'for' clause?

Sure, via the obvious spelling:

    [i for i in 1,1, 2,2,2, 3,3,3,3, 4,4]

Cleverness can't improve one that.

simplicity-is-a-feature-ly y'rs  - tim





More information about the Python-list mailing list