I have written a piece of code which basically does swap of first and last values in a List. Below are the methods.
In listMethods.py
def swapList(newList):
size = len(newList)
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
def swapList2(newList):
newList[0], newList[-1] = newList[-1], newList[0]
return newList
def swapList3(newList):
start, *middle, end = newList
newList = [end, *middle, start]
return newList
def swapList4(newList):
first = newList.pop(0)
last = newList.pop(-1)
newList.insert(0, last)
newList.append(first)
return newList
In main.py
import listMethods.py
n_list = [1, 2, 3, 4, 5]
print('Input to swapList 1 {}'.format(n_list)) #1,5
print('Output of swapList 1 {}\n'.format(listMethods.swapList(n_list))) #5,1
print('Input to swapList 2 {}'.format(n_list))
print('Output of swapList 2 {}\n'.format(listMethods.swapList2(n_list))) #5,1
print('Input to swapList 3 {}'.format(n_list))
print('Output of swapList 3 {}\n'.format(listMethods.swapList3(n_list))) #5,1
print('Input to swapList 4 {}'.format(n_list))
print('Output of swapList 4 {}\n'.format(listMethods.swapList4(n_list))) #5,1
Now, all the methods i call are alerting the main list which i pass to them except the last 3rd one which is swapList3. Why is it not updating the list value ?
Output:
Input to swapList 1 [1, 2, 3, 4, 5] Output of swapList 1 [5, 2, 3, 4, 1]
Input to swapList 2 [5, 2, 3, 4, 1] Output of swapList 2 [1, 2, 3, 4, 5]
Input to swapList 3 [1, 2, 3, 4, 5] Output of swapList 3 [5, 2, 3, 4, 1] #--- This value should have been given as input to swapList 4 but instead it is getting input as shown below
Input to swapList 4 [1, 2, 3, 4, 5] #--- wrong input to swapList 4 Output of swapList 4 [5, 2, 3, 4, 1]
Expected Output
Input to swapList 1 [1, 2, 3, 4, 5] Output of swapList 1 [5, 2, 3, 4, 1]
Input to swapList 2 [5, 2, 3, 4, 1] Output of swapList 2 [1, 2, 3, 4, 5]
Input to swapList 3 [1, 2, 3, 4, 5] Output of swapList 3 [5, 2, 3, 4, 1]
Input to swapList 4 [5, 2, 3, 4, 1] Output of swapList 4 [1, 2, 3, 4, 5]
Can someone help me understand methods in python
Tried to interchange the methods up and down. but the result is the same