XOR very big List with its rotation
<p>Efficient way to XOR on list such that,</p>

<p>eg:</p>

<pre><code>#here a,b,c,d are integers
L = [a,b,c,d]
N = [b,c,d,a] #right rotation of list L

Newlist = enter code here[a^b, b^c, c ^d ,d^a]
</code></pre>

<p>as size of list is very large, is there any efficient way to solve.</p>

<p>this is so far what i have done.</p>

<pre><code>#right rotation of list
def shift(seq, n):
    n = n % len(seq)
    return seq[n:] + seq[:n]

L = [6,7,1,3]
N = shift(L,1)
new = []
for i,j in zip(L,N):
    new.append(i^j)
print(new)
</code></pre>

<p>Here's another method!</p>

<p>I define a function that, given an index, returns the number at that index and its next neighbor on the "right" as a pair <code>(a, b)</code>, and then XOR those. It is also safe to give it indices outside the range of the list. So:</p>

<pre><code>def rotate_and_xor(l):
    def get_pair_xor(i):
        i %= len(l)
        j = (i + 1) % len(l)
        return l[i] ^ l[j]

    return list(map(get_pair_xor, range(len(l))))
</code></pre>

<p>I don't propose that this is necessarily the best solution; I just wanted to solve it a different way. Using list comprehensions like the others suggest is probably more Pythonic, but I like using <code>map</code>.</p>

python xor