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