Scope of a variable being changed locally in a Function_ python

Viewed 23

This function works correctly and is giving me the exact value of final_list when I am printing it. a

But when I decided to print final_list separately to check if the value is getting changed globally, I find out that the value of final_list is being changed locally

What to do so that final_list gets changed globally?

2 Answers

Use the keyword global in the function to make it a global variable.The changes within a function will be affected globally .

global variable

make these changes
global final_list in line 2 of your function.

As lists are mutable in Python. Don't add the line final_list = [ ] in your function and then run the code.

It will change the final_list.

Related