Search code examples
pythonlistlist-comprehensiongenerator

List comprehension with dynamic value changing


I have 2 variables. They stay in a reverse connection and form a list with 2 values which sum equals to 1. The example is shown below:

[0.1, 0.9], [0.2, 0.8], ..., [0.9, 0.1]

Is it possible to create a list generator that is able to do this?

So I want the following list of lists as a result:

[[0.1, 0.9], [0.2, 0.8], ..., [0.9, 0.1]]

I know, I can use a for loop for it, however, I'd like to know about this opportunity.


Solution

  • You can do something like

    [[z / 10, (10 - z) / 10] for z in range(1, 10)]
    

    This formulation attempts to reduce rounding error as much as possible.