Dict/List manipulation from C
Bernhard Herzog
herzog at online.de
Thu Aug 24 10:31:31 EDT 2000
"Richard Harvey" <tririch at connect.net> writes:
> (1) Replacing the contents of one dictionary with the contents of another.
> This is the case when I pass a dictionary from Python to my C code, using
> the dictionary as a "keyed" structure. Through other subsequent calls I
> create a new dictionary with matching entries, and want to return this back
> to the caller. Right now I'm having to retrieve the (key,value) list, then
> loop through each entry in the passed dictionary calling SetItem(key,value).
You could also use PyDict_Next. Unfortunately this function appears to
be undocumented.
> Seems a bit of overkill when in Python itself I can just say dict1 = dict2.
But dict1 = dict2 doesn't copy the dictionary. It just rebinds the dict1
variable. In C that would be
Py_DECREF(dict1);
dict1 = dict2;
Py_INCREF(dict1);
The refcounting may have to be different in your code, of course.
> (2) Removing all entries from a list.
PySequence_DelSlice(list, 0, PySequence_Length(list))
should do it, I think.
--
Bernhard Herzog | Sketch, a drawing program for Unix
herzog at online.de | http://sketch.sourceforge.net/
More information about the Python-list
mailing list