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 is another approach. The generator I've written could probably be improved, but it gives you the idea. This is space efficient, because you are not building a new list:</p>

<pre><code>&gt;&gt;&gt; def rotation(lst,n):
...   for i in range(len(lst)):
...     yield lst[(i + n) % len(lst)]
... 
&gt;&gt;&gt; L = [1,2,3,4,5]
&gt;&gt;&gt; list(rotation(L,1))
[2, 3, 4, 5, 1]
&gt;&gt;&gt; [a ^ b for a,b in zip(L,rotation(L,1))]
[3, 1, 7, 1, 4]
</code></pre>

<p>An alternative way to define <code>rotation</code> would be:</p>

<pre><code>&gt;&gt;&gt; def rotation(lst,n):
...   yield from (lst[(i + n) % len(lst)] for i in range(len(lst)))
... 
&gt;&gt;&gt; L = ['a','b','c','d']
&gt;&gt;&gt; ["{}^{}".format(i,j) for i,j in zip(L,rotation(L,1))]
['a^b', 'b^c', 'c^d', 'd^a']
</code></pre>

python xor