random.shuffle just exchanging elements, the line in which the exception occurred makes this very clear:
x[i], x[j] = x[j], x[i]
x - "", . i j range(0, len(x)), i j isn ' t, "", Exception. , , KeyError:
>>> import random
>>> d = {i: i for i in range(7, 10)}
>>> random.shuffle(d)
KeyError: 3
, , , range(0, len(x)):
>>> d = {i: i for i in range(10)}
>>> random.shuffle(d)
>>> d
{0: 7, 1: 9, 2: 3, 3: 4, 4: 0, 5: 2, 6: 1, 7: 6, 8: 8, 9: 5}
, , Exception. , :
d = {i: i for i in range(1, 10)}
random.shuffle(d)