title: Iterate a dict repeatedly/in a circle/ (simulate the data communication between sensor and data logger)
body: <p>I am designing a code snippet to replay logged sensor data. Can you help inspire me?
  The system works in this way:</p>
  <ul>
  <li>the sensor sends out 11 frames(one value in one frame) every 50ms.</li>
  <li>Randomly start and end data logging</li>
  </ul>
  <p>Here is the logged data:</p>
  <pre><code>dat = {'frame1':[0,1,2,3],
          'frame2':[0,1,2,3],
          'frame3':[0,1,2,3],
          'frame4':[0,1,2],
          'frame5':[0,1,2],
          'frame6':[0,1,2],
          'frame7':[0,1,2,3,4],
          'frame8':[0,1,2,3,4],
          'frame9':[0,1,2,3,4],
          'frame10':[0,1,2,3,4],
          'frame11':[0,1,2,3,4],
          }
  
  Correction: dat1 is from logfile_1, dat2 is from logfile_2, they are continuous from time perspective. 
  When start logging, data logger starts to write data into logfile_1, 
  the first cycle, data logger received frame7(value '0'), the sencond
   cycle , it received frame8(value '1'), until frame3(value '40'). the
   data logger will continue logging but will start to write data into 
  logfile_2 starting with frame4(value '42')
  
  dat = {'frame1':    [5,16,27,38],
          'frame2':   [6,17,28,39],
          'frame3':   [7,18,29,40],
          'frame4':   [8,19,30],
          'frame5':   [9,20,31],
          'frame6':   [10,21,32],
          'frame7': [0,11,22,33,],
          'frame8': [1,12,23,34],
          'frame9': [2,13,24,35],
          'frame10':[3,14,25,36],
          'frame11':[4,15,26,37],
          }
  
  dat2 = {'frame1':    [50,61,72,83],
          'frame2':    [51,62,73,84],
          'frame3':    [52,63,74,85],
          'frame4': [42,53,64,75,86],
          'frame5': [43,54,65,76,87],
          'frame6': [44,55,66,77,88],
          'frame7': [45,56,67,78,],
          'frame8': [46,57,68,79],
          'frame9': [47,58,69,80],
          'frame10':[48,59,70,81],
          'frame11':[49,60,71,82],
          }
  </code></pre>
  <p>To make playback working, my solution is to iterate the log file start from frame7 and iterate the whole dict 3 times(use a timer to control the cycle time). At last iterate first 3 frames. How to iterate the dict in this way?
  Or do you have a smart solution for this?
  Thanks in advance!</p>
  <h2><a href="https://i.stack.imgur.com/vfXI8.jpg" rel="nofollow noreferrer">logfile_1 and logfile_2</a></h2>
  <p>update the solution based on Alex's solution 1</p>
  <pre><code>def mock_sensor(dat):
      i = len('frame')
      end = dict(filter(lambda x: int(x[0][i:]) &gt;= 7, dat.items()))
      center = dict(filter(lambda x: (int(x[0][i:]) &lt; 7) and (int(x[0][i:]) &gt; 3), dat.items()))
      begin = dict(filter(lambda x: int(x[0][i:]) &lt;= 3, dat.items()))
      for frame in end:
          # do something
          print(frame)
          print(dat[frame][0])
          sleep(0.05,1)
      for _ in range(3):
          for frame in begin:
              print(frame)
              print(dat[frame][_])
              sleep(0.05,1)
          for frame in center:
              print(frame)
              print(dat[frame][_])
              sleep(0.05,1)
          for frame in end:
              print(frame)
              print(dat[frame][_+1])
              sleep(0.05,1)
      for frame in begin:
          print(frame)
          print(dat[frame][-1])
          sleep(0.05,1)
  
  mock_sensor(dat1)
  
  
  output:
  frame7
  0
  298391.1351247
  frame8
  1
  298391.1851848
  frame9
  2
  298391.23526
  frame10
  3
  298391.2853176
  frame11
  4
  298391.335383
  frame1
  5
  298391.3854675
  frame2
  6
  298391.4356516
  frame3
  7
  298391.4857041
  frame4
  8
  298391.5357598
  frame5
  9
  298391.5859605
  frame6
  10
  298391.6360342
  frame7
  11
  298391.6860827
  frame8
  12
  298391.7361375
  frame9
  13
  298391.7863534
  frame10
  14
  298391.8364631
  frame11
  15
  298391.8865663
  frame1
  16
  298391.9366365
  frame2
  17
  298391.9867175
  frame3
  18
  298392.0369269
  frame4
  19
  298392.0870066
  frame5
  20
  298392.1370775
  frame6
  21
  298392.187133
  frame7
  22
  298392.2372161
  frame8
  23
  298392.2873885
  frame9
  24
  298392.337439
  frame10
  25
  298392.3875374
  frame11
  26
  298392.4375916
  frame1
  27
  298392.4876377
  frame2
  28
  298392.5376943
  frame3
  29
  298392.5877566
  frame4
  30
  298392.637998
  frame5
  31
  298392.6880467
  frame6
  32
  298392.7380987
  frame7
  33
  298392.788183
  frame8
  34
  298392.8382282
  frame9
  35
  298392.8882728
  frame10
  36
  298392.938336
  frame11
  37
  298392.9884798
  frame1
  38
  298393.0385387
  frame2
  39
  298393.0885821
  frame3
  40
  298393.1386231
  </code></pre>