xxxxxxxxxx
# suppose the list is x:
return [x[i:i+2] for i in range(0, len(x), 2)]
# or, as a generator
return x[i:i+2]for i in range(0,len(x),2)
xxxxxxxxxx
in python below is how you swap 2 elements of list
x[i+1],x[i]=x[i],x[i+1]
Don't use function swap(user defined or pre-defined)
xxxxxxxxxx
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2]=list[pos2], list[1]
return list
List=[1,3,5,7,9,11]
pos1, pos2=1,3
print(swapPositions(list, pos1-1, pos2-1))