list comprehension question
John Machin
sjmachin at lexicon.net
Sun Mar 24 16:51:38 EST 2002
Tripp Scott <tripps81 at yahoo.com> wrote in message news:<mailman.1016945436.31630.python-list at python.org>...
> can i generate this list:
>
> [1, 1.1, 2, 2.1, 3, 3.1]
>
> with a list comprehension in a form of this:
>
> [SOMETHING for x in 1,2,3]
>
> and without using side effects like this?
>
> result=[]
> [(result.append(x), result.append(x+1.1)) for x in 1,2,3]
>
> because the latter case is not very elegant to me.
>
> t
This doesn't match exactly what you asked for, but may match what you meant
to ask for, and certainly it clearly states what is happening:
>>> [i + delta for i in 1, 2, 3 for delta in 0, 0.1]
[1, 1.1000000000000001, 2, 2.1000000000000001, 3, 3.1000000000000001]
... and you may in fact have meant to ask for this:
>>> [i + delta for i in 1, 2, 3 for delta in 0.0, 0.1]
[1.0, 1.1000000000000001, 2.0, 2.1000000000000001, 3.0, 3.1000000000000001]
More information about the Python-list
mailing list