Deleting an item from an argsList

LyTning94

Dragonborn
Joined
Nov 10, 2010
Messages
397
Location
Skyrim
I have an argsList which is passed to my python function from the SDK, and I need to delete the first item in that list. So I used:

Code:
del argsList[0]

but I got a python exception saying "item does not support object deletion".

How else do I go about deleting it from the list???
 
Firstly, there should be no reason for you to delete anything from that tuple. Secondly, tuples (unlike list arrays) are non-mutable data structures, so you can't add anything to or delete anything from them.

If you really wanna do this, and I doubt you do, you would have to make a copy of the old tuple, leaving out the first entry. Then replace the old tuple with the new one. (I would have to look the exact syntax up in the Python documentation.)
 
Thanks for the help.

@Baldyr
This is my own argsList I'm passing from a new function I added in the SDK. The goal of the python function is to write the items in the argsList to a file, but the first item in the list determines whether to overwrite what's already in the file, or to append. I don't need this first item in the file, which is why I'm deleting it from the list.
 
Well, if you're gonna save the contents as separate values, you can just omit the first one. Only save the second, third, fourth one, and so on. This seems easier than to save a whole array, which would have to be picked into string type before saving. Individual values can be converted into string format with the str() function.
 
I don't really know much about Python, so maybe this isn't the best way to go, but I'm using pickle.dump() to write the whole list to the file. That's why I needed the first item removed.
 
I understand how it would be awkward to store the value that determines whether or not to store the array in the first place, but I can't see why you can't do it. Its only a True/1 or False/0 value and hardly takes up any space on disc. The script needed to get rid of it before storing would probably take up as much processing power than it takes to pickle it along with the rest of the values.

So this might really be a non-issue. But if you describe in more detail what you're trying to do and why, someone would be able to give you the optimal solution.
 
Since it only happens when writing to disk, processing time is not really a factor. Writing to disk takes much longer.
 
I'm not really worried about processing time. I'll try and explain exactly why I wanted the value gone:

I'm adding specific unit information for several different units to the argsList in the SDK. This I am, in turn, passing to Python. However, since an argsList can only hold 20 arguments, if the number of units exceeds a certain number I have to pass it to Python, clear the argsList, and then continue with a new argsList. The first time I want to overwrite what was in the file before, but on subsequent times I want to append.

I only add the overwrite/append value at the beginnning of the argsList, not before each individual unit's information. If I have multiple argsLists which are being pickled to the file, then these 0 or 1s will then appear at varying intervals, which will mess up the order of unit info (I am going to be reconstructing these units from the info).

So basicallly I'm just doing it for ease in reading the file later.
 
In order to only pickle the subsequent entries you can just go:
Code:
import pickle
data = pickle.dumps(argsList[1:])
Now you can save the data in string format.
 
What are the advantages of string format?
 
Pickling turns stuff (like a argsList tuple data-structure) into string format. String format can be written to disk...

But if you don't need to write the whole data-structure as such on disk, but rather save the individual values as such, then you don't need to pickle anything. Using the str() function will do the trick, but then you of course need to use it on each value separately.

Note however that you can't pickle Cy class objects for reasons hysterical. Instead you need to use pairs of integers that refer to the ID of the instance and the owner's ID. Holler if you run into troubles.
 
Thanks for the explanation. And yeah, I've only been pickling integers.
 
Back
Top Bottom