0

how would I go about preventing an action done in a function from resetting? for example:

def fun(list_1, list_2):
    list_2 = list(list_1)
    print(list_2)
    return list_2

if __name__ == '__main__':
    list_1 = [[1, 2, 3], [4, 5, 6]]
    list_2 = []
    fun(list_1, list_2)

    print(list_1)
    print(list_2)

    print("Done")

I would like to be able to access the value of list_2 after exiting the function but it keeps resetting it. Is there a way to go about doing that because I'm restricted from using global on a project I'm working on? Thanks and have a great day.

leem
  • 1
  • 1

1 Answers1

2

when you call the fun(list_1, list_2), store it in a variable. Once in a variable, you can access this value whenever you choose

new_list_2 = fun(list_1, list_2)
print(new_list_2)

Alternatively, you can reassign the list_2 variable to point to the returned value of the function.

list_2 = fun(list_1, list_2)
print(list_2)
Arky Asmal
  • 1,162
  • 4
  • 10