This code will switch the index position of two items with each other. Same code can be used in same way for more than 2 items, so far even number of items need swappingÂ
def SwapItemPos(MyUpList,Item1,Item2):
X,Y = MyUpList.index(Item1),MyUpList.index(Item2)
MyUpList[Y],MyUpList[X] = MyUpList[X], MyUpList[Y]
print MyUpList
MyList = [‘One’, ‘Two’, ‘Three’, ‘Four’, ‘Five’]
Item1 = MyList[2]
Item2 = MyList[3]
SwapItemPos(MyList,Item1,Item2)
Output:
>>> [‘One’, ‘Two’, ‘Four’, ‘Three’, ‘Five’]