You can use list.insert
to help with left shift and right shift.
list.pop
, removes the element from the original list and returns it as well. list.insert
adds the returned element into the list at given index (0 or -1 in this case). NOTE: this operation is in place!
#Left shift
mylist = ['apples', 'guitar', 'shirt']
mylist.insert(-1,mylist.pop(0))
mylist
### ['guitar', 'apples', 'shirt']
#Right shift
mylist = ['apples', 'guitar', 'shirt']
mylist.insert(0,mylist.pop(-1))
mylist
### ['shirt', 'apples', 'guitar']
A better way to do this is with collections.deque
. This will allow you to work with multiple shifts and has some other neat queue functions available as well.
from collections import deque
mylist = ['apples', 'guitar', 'shirt']
q = deque(mylist)
q.rotate(1) # Right shift ['shirt', 'apples', 'guitar']
q.rotate(-1) # Left shift ['guitar', 'shirt', 'apples']
q.rotate(3) #Right shift of 3 ['apples', 'guitar', 'shirt']
EDIT: Based on your comments, you are trying to get permutations -
from itertools import product
[i for i in product(l, repeat=2) if len(set(i))>1]
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
OR
out = []
for i in l:
for j in l:
if len(set([i,j]))>1:
print(i,j)
a b
a c
b a
b c
c a
c b