G+_Jason Perry Posted October 9, 2014 Share Posted October 9, 2014 so I realize we are working on C# but I was wondering if someone can help me out with what should be a simple python question. I want to clear out the data in a list after I am done using it. for Line in RawFileList RawFileList[Line] = "" I am using RawFileList to store the raw data from a file to be processed and spit out into another file. Link to comment Share on other sites More sharing options...
G+_Nate Follmer Posted October 9, 2014 Share Posted October 9, 2014 del RawFileList[:] <- This will clear the list completely. Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted October 9, 2014 Share Posted October 9, 2014 Why do you think you need to clear the list? Just clear the variable: RawFileList = [] This makes the variable point to an empty list, and the original list becomes unreferenced, and will be reclaimed by the garbage collector. Link to comment Share on other sites More sharing options...
G+_Jason Perry Posted October 9, 2014 Author Share Posted October 9, 2014 how do I just clear the variable? RawFileList = "" Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted October 9, 2014 Share Posted October 9, 2014 That would do it too. As would "RawFileList = 5”, but using an empty list rather than a string or number seemed like the most lucid thing to do. Link to comment Share on other sites More sharing options...
G+_Jason Perry Posted October 9, 2014 Author Share Posted October 9, 2014 Well that seems to have done it. Just for future reference am I going to run into any problems when I try to populate the variable again with a data file that has a different number of items or did that wipe out everything? Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted October 10, 2014 Share Posted October 10, 2014 It didn't "wipe out" anything. Variables are just names for things in Python--the things themselves exist independently. When you built up that list from a file, memory was allocated for all the values and the list made to point to them. Now the variable points to an empty list. The old list is still in memory, but since it has no name (no Python variable points to it) anymore, the garbage collector can reclaim that memory. Or it may decide not to until it needs the memory. That's what garbage collection is all about--you don't need to do the memory management yourself. Link to comment Share on other sites More sharing options...
G+_Jason Perry Posted October 10, 2014 Author Share Posted October 10, 2014 I am just so use to variables having fixed parameters that you just cant set the entire variable to nothing, I would have to set every entry to nothing, or let it decide what is in there. Link to comment Share on other sites More sharing options...
G+_Rik Schreurs Posted October 12, 2014 Share Posted October 12, 2014 Yes, it takes some getting used to, if you're coming from another language like C or Visual Basic. But this is really how Python works. If you have a list with things in it and you want to start over, the simplest way is to assign a new, empty list to that variable. (Like Lee said, it's just a name that refers to some object.) Don't worry about cleaning stuff up, that's a job for the garbage collector. Link to comment Share on other sites More sharing options...
G+_Barry Fishman Posted October 12, 2014 Share Posted October 12, 2014 As others have said, you don't usually need to clear out things after you use them. The garbage collector does that when the variable is no longer used, or when the variable is set to something else. If you have some special need, such as wanting to make sure data is no longer referenced, you can usually just set it to None. Technically you can do: del RawFileList Which will cause an error to be signalled if your code uses the variable without giving it a new value. Link to comment Share on other sites More sharing options...
Recommended Posts