In the next few videos about Python dictionaries, Tim demonstrates the ordering of dictionary keys changing, each time he runs the program.
That behaviour has now changed. Since Python version 3.6, the original ordering of the keys is preserved.
That means you won't see random behaviour, when printing the dictionary keys.
The advice in the lectures is still valid: if your code relies on processing the dictionary keys in a specific order, then sort them first.
When you've learnt about dictionaries, in the next few videos, try this code. It demonstrates why you can't rely on the dictionary remaining sorted, when you add keys to a dictionary. The ordering is preserved, but insertion order is often not what you want:
numbers = {
1: 'one',
3: 'three',
5: 'five',
7: 'seven',
9: 'nine',
}
print("I can count odd numbers in order")
for key in numbers:
print(numbers[key])
numbers[8] = 'eight'
numbers[2] = 'two'
numbers[6] = 'six'
numbers[4] = 'four'
print()
print("I can count to 9 in order")
for key in numbers:
print(numbers[key])
# If your code relies on the keys being sorted, sort them first
print()
print("I really can")
for key in sorted(numbers):
print(numbers[key])