Home > front end >  Scope of a variable being changed locally in a Function_ python
Scope of a variable being changed locally in a Function_ python

Time:09-21

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?

CodePudding user response:

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.

CodePudding user response:

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