The dict is data = {1: 0, 2: 1, 3: 2, 0: 1} and x starts at 0. len(data) is 4, so the loop runs exactly four times, each time doing x = data[x]. Tracing: start x=0 -> data[0]=1 -> data[1]=0 -> data[0]=1 -> data[1]=0. After the fourth iteration x is 0, so print(x) outputs 0. The value simply oscillates between 0 and 1, and an even number of steps lands back on 0.
Community Discussion