title: Python: append a list from a dictionary with conditions
body: <p>I have a dictionary from which I want to filter out and append the results in another dictionary. The condition is if the difference between two first elements (for example 31 - 30  = 1) in the dictionary is smaller than 5 then add the associated second element of the dictionary and append it in a new dictionary else keep the same first element with the associated second element.</p>
  
  <pre><code>a = {"20" : "1.5", "30" : "2.0", "31" : "1.0", "40" : "1", "50" : "1.5"}
  listb = []
  listc = []
  newdict = {}
  for key in a:
      b = key
      c = a[key]
      listb.append(b)
      listc.append(c)
  
  for i in range(len(listb)):
      low = listb[i]
      high = listb[i+1]
      diff = int(high) - int(low)
  #     print(low)
      if (diff &gt; 5):
          num = listc[i]
  #         print(num)
          num_a = listb[i]
  #         print(num_a)
          newdict[[num_a][i]] = num
          print((newdict))
      else:
          num = listc[i] + listc[i+1]
          print(num)
          num_a = listb[i+1]
          print(num_a)
          newdict[[num_a][i]] = num
  print(newdict)
  </code></pre>
  
  <p>The output of this should look something like</p>
  
  <p>a = {"20" : "1.5", "31" : "3.0", "40" : "1", "50" : "1.5"}</p>