6

I have a question that how to clear a list that's formed by PyList_Append()? Is there a document about Python/C extension API functions in detail?

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
sophistcxf
  • 111
  • 3
  • Thanks, but another question. PyObject *newlist = PyList_New(0); PyObject *subList = PyList_New(0); PyList_Append(subList, PyString_FromString("first")); PyList_Append(subList, PyString_FromString("second")); PyList_Append(subList, PyString_FromString("third")); PyList_SetSlice(subList, 0, PyList_Size(subList), NULL); PyList_Append(subList, PyString_FromFormat("abcdef")); PyList_Append(newlist, subList); now I get [['abcdef'],['abcdef']]. The initial strings "first" and "second" are override. How to solve it? Sorry I don't know how to edit the format. Maybe I need a tutorial. – sophistcxf May 06 '14 at 10:38

2 Answers2

7

IIRC you have to use PyList_SetSlice:

PyList_SetSlice(your_list, 0, PyList_Size(your_list), NULL);
sloth
  • 99,095
  • 21
  • 171
  • 219
6

You can use the PySequence_DelSlice function:

# The same as: del L[0:len(L)]
PySequence_DelSlice(L, 0, PySequence_Length(L));
vz0
  • 32,345
  • 7
  • 44
  • 77