3 ms·
This, maybe ```py x = [xx for x in [1, 2, 3, 4] if xx < 10] ```
by kangalioo 5y ago
This, maybe
```py
x = [xx for x in [1, 2, 3, 4] if xx < 10]
```
- williamstein 5y agoI thought what you wrote wasn't valid because xx isn't defined in the scope of the inequality... however I now realize that's because Hacker News deletes the asterisks. In any case, it's [x [asterisk] x for x in [1,2,3,4] if x[asterisk] x < 10] which is still a little annoying because the x [asterisk] x gets computed twice. This is one of those little details that really annoys mathematicians, as I saw with SageMath for decades (I started SageMath -- which uses Python -- and got no end of complaints about the above). In the Magma programming language there is a "where" version of comprehensions that eloquently solves this problem as explained here: http://magma.maths.usyd.edu.au/magma/handbook/text/10 http://magma.maths.usyd.edu.au/magma/handbook/text/10 For example, { xx : x in [1 .. 4] | xx lt 10 where xx is x*x} which you can try at http://magma.maths.usyd.edu.au/calc/ http://magma.maths.usyd.edu.au/calc/
- dragonwriter 5y ago> however I now realize that’s because Hacker News deletes the asterisks You can escape asterisks with a backslash. \* => *