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>You don't have to worry about resetting the multiplier if you use <a href="https://docs.python.org/2/library/itertools.html#itertools.cycle" rel="nofollow"><code>itertools.cycle</code></a>:</p>

<pre><code>&gt;&gt;&gt; itertools.cycle(range(2, 10))
2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, ...
</code></pre>

<p>so you can simplify your function like below:</p>

<pre><code>from itertools import cycle

def mod11(lst, max_weight=9):
    multipliers = cycle(range(2, max_weight + 1))
    zipped = zip(lst, multipliers))
    summed = sum(a * b for a, b in zipped)
    mod = 11 - (summed % 11)
    return (0 if mod &gt; 9 else mod)
</code></pre>

python