Remove and return value at `index` in sorted list. Raise :exc:`IndexError` if the sorted list is empty or index is out of range. Negative indices are supported. Runtime complexity: `O(log(n))` -- approximate. >>> sl = SortedList('abcde') >>> sl.pop
(self, index=-1)
| 1317 | |
| 1318 | |
| 1319 | def pop(self, index=-1): |
| 1320 | """Remove and return value at `index` in sorted list. |
| 1321 | |
| 1322 | Raise :exc:`IndexError` if the sorted list is empty or index is out of |
| 1323 | range. |
| 1324 | |
| 1325 | Negative indices are supported. |
| 1326 | |
| 1327 | Runtime complexity: `O(log(n))` -- approximate. |
| 1328 | |
| 1329 | >>> sl = SortedList('abcde') |
| 1330 | >>> sl.pop() |
| 1331 | 'e' |
| 1332 | >>> sl.pop(2) |
| 1333 | 'c' |
| 1334 | >>> sl |
| 1335 | SortedList(['a', 'b', 'd']) |
| 1336 | |
| 1337 | :param int index: index of value (default -1) |
| 1338 | :return: value |
| 1339 | :raises IndexError: if index is out of range |
| 1340 | |
| 1341 | """ |
| 1342 | if not self._len: |
| 1343 | raise IndexError('pop index out of range') |
| 1344 | |
| 1345 | _lists = self._lists |
| 1346 | |
| 1347 | if index == 0: |
| 1348 | val = _lists[0][0] |
| 1349 | self._delete(0, 0) |
| 1350 | return val |
| 1351 | |
| 1352 | if index == -1: |
| 1353 | pos = len(_lists) - 1 |
| 1354 | loc = len(_lists[pos]) - 1 |
| 1355 | val = _lists[pos][loc] |
| 1356 | self._delete(pos, loc) |
| 1357 | return val |
| 1358 | |
| 1359 | if 0 <= index < len(_lists[0]): |
| 1360 | val = _lists[0][index] |
| 1361 | self._delete(0, index) |
| 1362 | return val |
| 1363 | |
| 1364 | len_last = len(_lists[-1]) |
| 1365 | |
| 1366 | if -len_last < index < 0: |
| 1367 | pos = len(_lists) - 1 |
| 1368 | loc = len_last + index |
| 1369 | val = _lists[pos][loc] |
| 1370 | self._delete(pos, loc) |
| 1371 | return val |
| 1372 | |
| 1373 | pos, idx = self._pos(index) |
| 1374 | val = _lists[pos][idx] |
| 1375 | self._delete(pos, idx) |
| 1376 | return val |