0

I want to clear all variables in my code except the loop index after every iteration. for example

for i=1:20
c= i+20; 
save c;
clearvars -except i
end

How do I achieve this in Python?

braX
  • 10,905
  • 5
  • 18
  • 32
Farouk Yahaya
  • 33
  • 1
  • 8

1 Answers1

0

A good solution is to define a function, and then only return the variables you want to keep. In Python, variables that are defined within the scope of a function are removed when the function is done executing.

def my_for(i):
    for j in range(1, i+1):
        c = j + 20
    return i
James
  • 29,484
  • 4
  • 43
  • 62
  • the problem is that. i work with dictionaries and pickle. and i have a nested 2 loops. so after the first loop finishes and have saved all variables to pickle. i want to clear to avoid any problems. – Farouk Yahaya Nov 19 '19 at 10:38