Search code examples
pythonlistfunctionreturn

How to properly return a list back to my main() function


After passing an updated list back to my main() function, the list from my function is not stored and it just prints the original list before it was modified by my function. It seems to work fine for the other functions in my program, just not this one.

Here is the code for shifting a list over to the right by one

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

Here is the function call in my main()

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

output is:

[10, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
After shifting right:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The first line is the expected output and is printed directly from the function. The second line is the output from the print() statement in main() after the function is called. I am not allowed to change the main() function as part of my assignment so I'm a little stumped on how to proceed. I have around 10 functions that all return data and only 8 of them work as intended.

Thanks!


Solution

  • In your main you have to catch the output of function see below example:

    data = list(ONE_TEN)
    data = shiftRight(data) # here data is overwritten by data from shiftRight function
    print("After shifting right: ", data)