Repository navigation
Expand file tree
/
Copy pathordereddict.py
More file actions
45 lines (35 loc) 路 1.66 KB
/
Copy pathordereddict.py
File metadata and controls
45 lines (35 loc) 路 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
An `OrderedDict` (`collections.OrderedDict`) remembers the order in which
entries were added, like a regular `dict`. Its distinguishing feature is that
it also provides methods for changing and removing entries by their position.
Regular dictionaries preserve insertion order starting with Python 3.7, but
they do not have `move_to_end()` or allow `popitem(last=False)` to remove the
first entry.
"""
from collections import OrderedDict
def main() -> None:
# Both dict and OrderedDict preserve insertion order
regular: dict[str, int] = {"first": 1, "second": 2, "third": 3}
ordered: OrderedDict[str, int] = OrderedDict(regular)
assert list(regular) == ["first", "second", "third"]
assert list(ordered) == ["first", "second", "third"]
# Updating a value does not change the key's position in either mapping
regular["first"] = 10
ordered["first"] = 10
assert list(regular) == ["first", "second", "third"]
assert list(ordered) == ["first", "second", "third"]
# With dict, deleting and reinserting a key moves it to the end
del regular["first"]
regular["first"] = 10
assert list(regular) == ["second", "third", "first"]
# OrderedDict can move an existing key directly to the end or beginning
ordered.move_to_end("first")
assert list(ordered) == ["second", "third", "first"]
ordered.move_to_end("first", last=False)
assert list(ordered) == ["first", "second", "third"]
# popitem() removes the newest; last=False removes the oldest
assert ordered.popitem() == ("third", 3)
assert ordered.popitem(last=False) == ("first", 10)
assert list(ordered) == ["second"]
if __name__ == "__main__":
main()