Skip to content

Commit

Permalink
[stdlib] Honours the Python dict.popitem() LIFO ordering
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Saelices <[email protected]>
  • Loading branch information
msaelices committed May 30, 2024
1 parent 2e45b91 commit a034f7b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion stdlib/src/collections/dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ struct Dict[K: KeyElement, V: CollectionElement](
"""
if self.size == 0:
raise "KeyError: popitem(): dictionary is empty"
var entry = self._entries.pop(0)
var entry = self._entries.pop(self.size - 1)
var entry_value = entry.value()
self.size -= 1
self._n_entries -= 1
Expand Down
8 changes: 4 additions & 4 deletions stdlib/test/collections/test_dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ def test_popitem():
dict["b"] = 2

var t1: (String, Int) = dict.popitem()
assert_equal(t1[0], "a")
assert_equal(t1[1], 1)
assert_equal(t1[0], "b")
assert_equal(t1[1], 2)
assert_equal(1, len(dict))

var t2: (String, Int) = dict.popitem()
assert_equal(t2[0], "b")
assert_equal(t2[1], 2)
assert_equal(t2[0], "a")
assert_equal(t2[1], 1)
assert_equal(0, len(dict))

with assert_raises(contains="KeyError"):
Expand Down

0 comments on commit a034f7b

Please sign in to comment.