Documentation Index
Fetch the complete documentation index at: https://mintlify.com/python/cpython/llms.txt
Use this file to discover all available pages before exploring further.
Concrete Objects
Dictionary Objects
Working with Python dict type
⌘I
Working with Python dict type
Documentation Index
Fetch the complete documentation index at: https://mintlify.com/python/cpython/llms.txt
Use this file to discover all available pages before exploring further.
PyObject* PyDict_New(void)
PyObject *dict = PyDict_New();
return dict;
PyObject* PyDict_GetItem(PyObject *p, PyObject *key) // Borrowed!
PyObject* PyDict_GetItemString(PyObject *p, const char *key) // Borrowed!
int PyDict_Contains(PyObject *p, PyObject *key)
PyDict_GetItem returns borrowed reference - don’t Py_DECREF!PyObject *key = PyUnicode_FromString("name");
PyObject *value = PyDict_GetItem(dict, key);
Py_DECREF(key);
if (value != NULL) {
// Use value (borrowed reference)
}
int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
int PyDict_DelItem(PyObject *p, PyObject *key)
void PyDict_Clear(PyObject *p)
PyObject *key = PyUnicode_FromString("age");
PyObject *val = PyLong_FromLong(25);
PyDict_SetItem(dict, key, val);
Py_DECREF(key);
Py_DECREF(val);
int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Py_ssize_t pos = 0;
PyObject *key, *value;
while (PyDict_Next(dict, &pos, &key, &value)) {
// key and value are borrowed references
printf("Key: %s\n", PyUnicode_AsUTF8(key));
}
PyObject* PyDict_Keys(PyObject *p)
PyObject* PyDict_Values(PyObject *p)
PyObject* PyDict_Items(PyObject *p)
