Show that Python dictionaries preserve insertion order, and what happens when you update or re-add a key.
Dictionaries preserve insertion order: iterating a dict yields the keys in the order they were first added.
data = {}
data["b"] = 2
data["a"] = 1
data["c"] = 3
print(list(data)) # ['b', 'a', 'c'] - insertion order
# Updating a key keeps its original position
data["b"] = 20
print(list(data)) # ['b', 'a', 'c']
# Deleting and re-adding moves the key to the end
del data["b"]
data["b"] = 20
print(list(data)) # ['a', 'c', 'b']The first line shows insertion order rather than sorted order, so the dictionary is not alphabetical. The second shows that updating an existing key does not move it — the position it was first inserted at is kept. The third shows that deleting a key and adding it again puts it at the end, because that is a fresh insertion.
Two consequences worth stating. First, order is stable and reproducible, so two dictionaries built by the same code iterate in the same sequence, which makes log output and test assertions predictable. Second, order is guaranteed but it is not a sorting guarantee — if you need alphabetical order you must ask for it explicitly with sorted(data) or sorted(data.items()).
The same rules apply to the keys, values and items views, since they all iterate in the same underlying order. And the same guarantee exists for sets only in the weak sense that set iteration order is deterministic for a given set of values within a process, but it is not insertion order and must not be relied on.