Multiply number list by range of numbers
<p>I'm trying to write code for a modulus 11 but I'm failing to make it more python-like.</p>

<p>As of now I'm using a weight and increasing it, checking when it reaches a number and then set it to it's original value.</p>

<p>Let's say I have a list of numbers
<code>1..20</code> and I'd like to multiply them by <code>2,3,4,5,6,7,8,9,2,3,4,etc</code> so that each index would be multiplied by an increasing number.</p>

<p>1x2, 2x3, 3x4, 4x5, 5x6, 6x7, 7x8, 8x9, 9x2, 10x3, etc..</p>

<p>Is there an elegant way to do this?</p>

<p>Unelegant way:</p>

<pre><code>def mod11(list, max_weight=9):
    sum = 0
    weight = 2

    for item in reversed(list):
        sum += item * weight
        weight += 1

        if weight &gt; max_weight:
            weight = 2

    mod = 11 - sum % 11

    if mod &gt; 9:
        return 0

    else:
        return mod
</code></pre>

<p>Ok, I am not quite sure if I understand your question correctly, since you talk about modulus 11, but also seem to use numbers from 2 up to and including 9.</p>

<p>But let's first start with writing the 1..20 as a list, that would be possible as follows:</p>

<pre><code>list(range(1,21))    (This is if you want to include the 20)
</code></pre>

<p>Next, to turn some integer into a number between 2 and 9 you could do the following:</p>

<pre><code>x -&gt; x % 8 + 2
</code></pre>

<p>Combining these two, you could make a list comprehension:</p>

<pre><code>[((x-1) % 8 + 2) * x for x in xrange(1,21)]
</code></pre>

<p>(the -1 is added to start the first number from 2 instead of from 3)</p>

python