0

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

Jacob
  • 21
  • 3
  • because nowhere in that function do you mutate the list. Where do you think the list should be mutated in that function? – juanpa.arrivillaga Jun 09 '23 at 16:13
  • as an aside, a terminology note, those are *functions* not methods. – juanpa.arrivillaga Jun 09 '23 at 16:15
  • 1
    I’m voting to close this question because This is not a tutoring service. Please see: [Why is Can someone help me? not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) for more details. – itprorh66 Jun 09 '23 at 16:41
  • newList = [end, *middle, start] - This is the part i am mutating the list @juanapa.arriviallaga – Jacob Jun 12 '23 at 07:48
  • My answer is found in this page. https://stackoverflow.com/questions/31359652/how-to-mutate-a-list-with-a-function-in-python Hence we can close this query – Jacob Jun 12 '23 at 08:06

1 Answers1

2

You cannot mutate a list using simple assignment operator inside a function. Instead you can mutate using [:]. Refer below link for more details.

How to mutate a list with a function in python?.

Many thanks for the support.

It all depends on what you are actually doing, appending or any direct mutation of the list will be reflected outside the function as you are actually changing the original object/list passed in. If you were doing something that created a new object and you wanted the changes reflected in the list passed in setting s[:] =.. will change the original list.

Jacob
  • 21
  • 3
  • Links often die without warning, please consider adding some details from the page you linked to your answer, while keeping the link as a reference – iggy12345 Jun 12 '23 at 17:49
  • 1
    Done https://stackoverflow.com/users/10804423/iggy12345 – Jacob Jun 14 '23 at 07:56