Remove and return a (key, value) pair from the dictionary. Pairs are returned in LIFO order if last is true or FIFO order if false.
(self, last=True)
| 169 | dict.clear(self) |
| 170 | |
| 171 | def popitem(self, last=True): |
| 172 | '''Remove and return a (key, value) pair from the dictionary. |
| 173 | |
| 174 | Pairs are returned in LIFO order if last is true or FIFO order if false. |
| 175 | ''' |
| 176 | if not self: |
| 177 | raise KeyError('dictionary is empty') |
| 178 | root = self.__root |
| 179 | if last: |
| 180 | link = root.prev |
| 181 | link_prev = link.prev |
| 182 | link_prev.next = root |
| 183 | root.prev = link_prev |
| 184 | else: |
| 185 | link = root.next |
| 186 | link_next = link.next |
| 187 | root.next = link_next |
| 188 | link_next.prev = root |
| 189 | key = link.key |
| 190 | del self.__map[key] |
| 191 | value = dict.pop(self, key) |
| 192 | return key, value |
| 193 | |
| 194 | def move_to_end(self, key, last=True): |
| 195 | '''Move an existing element to the end (or beginning if last is false). |