I'm new to python and am trying to make a function that swaps multiple values in a list at once.
def swap(deck: List[int], start1: int, end1: int, start2: int, end2: int) -> None:
start1 = start1 % len(deck)
start2 = start2 % len(deck)
end1 = end1 % len(deck)
end2 = end2 % len(deck)
if start1 < start2:
deck[start1: end1], deck[start2: end2] = deck[start2: end2], deck[start1: end1]
else:
deck[start2: end2], deck[start1: end1] = deck[start1: end1], deck[start2: end2]
when deck = [0,1,2,3,4,5,6,7,8,9,10]
swap(deck, -3, 11, 0, 2)
should mutate the deck to be [8,9,2,3,4,5,6,7,0,1,10]
, but I get this instead [2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 10]
I have also tried it with the temp variable method
temp = deck[start1: start1]
deck[start1: start1] = deck[start2: end2]
deck[start2: end2] = temp
but I get the same result... An explanation of why this is happening and how I can fix it is greatly appreciated.