0

My professor assigned a project in which we perform a series of alterations to a list. We are provided with a main function such as:

CURRENT_LIST = [1,2,3,4,5]

def main():
    data = list(CURRENT_LIST)
    shiftRight(data)
    print("After shifting data", data)

Basically, we have to write functions that handle these functions that main() is calling. I understand how to write the functions. For example, shifting everything to the right would yield something like:

def shiftRight(data:list) -> (list):
     data = data[-1:] + data[:-1]
     return(data)

My question is: is it possible to do this without altering the main() function we were provided with? From my understanding, after calling shiftRight(), I would have to assign it to the list again. Something like:

   data = shiftRight(data)

But the main function we were provided with is not set up like that. I am wondering if I can reassign the data list without modifying the main() function. I would like to avoid using global variables as well, as I don't think that was my professor's intention.

Has he made a mistake in the main() we were provided with? Or is there truly a way to modify this list without using global variables and without modifying main()?

Alec
  • 8,529
  • 8
  • 37
  • 63

2 Answers2

1

You can modify the content of the list, this will make an update to the original list that is pointer by the variable instead of creating a new one:

>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> def shiftRight(l):
...     l[:] = l[-1:] + l[:-1]
... 
>>> shiftRight(l)
>>> l
[9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
Netwave
  • 40,134
  • 6
  • 50
  • 93
  • Interesting, it works. My only question is: why? From my understanding, if you have a list, "somelist", then "somelist[:]" refers to a copy of that list. So, how is shiftRight(l) working? It looks to me like it's creating a copy of the list with alterations of the pointed list. I am curious about the backend of the solution, albeit it works. Thank you. – 127dot0dot0dot1 Apr 22 '19 at 05:05
  • @127dot0dot0dot1, you have a thread explaining that: https://stackoverflow.com/a/10623352/1695172, it is really interesting :) – Netwave Apr 22 '19 at 05:09
  • Very interesting and well-articulated thread. Thank you for answering all my questions! – 127dot0dot0dot1 Apr 22 '19 at 06:06
0

The main function looks at CURRENT_LIST, makes a copy of it, and then calls that copy data. It then passes data to a function called shiftRight, which accepts a list and performs a right shift on that list. This function mutates the list in-place and returns None (which happens by default if you omit a return statement). Then main prints the result.

The def shiftRight... you have there should be placed outside the main function, right after the assignment of CURRENT_LIST. You will also need to tweak that shiftRight function to mutate the passed list rather than creating and returning a new one. Try the pop and insert list methods for this.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97