title: How to overcome nested for loop to achieve a linear time complexity?
body: <p>First of all this code compare the two dictionaries and puts it into one bigger dictionary. 
  1. If it has the same key, put the value from two dictionaries into one key. 
  2. If it has a similar key (e.g. banana &amp; banan4), also put the value from two dictionaries into one key and remove it. 
  3. Lastly, if it has a different value than put the value according to the key of its original dictionary.</p>
  
  <pre><code>import pandas as pd
  from fuzzywuzzy import fuzz
  
  dict1 = {'apple' : 'g1', 'banana' : 'g2', 'mango' : 'g3'}
  dict2 = {'apple' : 'a3', 'banana' : 'a4', 'grapes' : 'a1', 'pear' : 'a2', 'banan4' : 'a5'}
  
  merged = {}
  blocked_tuple = (dict1, dict2)
  threshold = 59
  
  for new_dict in blocked_tuple:
      for key in new_dict:
          flag = True
          if key in merged:
              merged[key].append(new_dict[key])
              flag = False
          else:
              merged[key] = [new_dict[key]]
          while flag:
              try:
                  for i in merged:
                      distance = fuzz.token_set_ratio(key, i)
                      if (distance &gt; threshold) and (distance != 100):
                          merged.pop(key)
                          merged[i].append(new_dict[key])
                          flag = False
                      else: flag = False
              except: pass
  
  print(merged)
  </code></pre>
  
  <p>This code will output:</p>
  
  <pre><code>{'apple': ['g1', 'a3'], 'banana': ['g2', 'a4', 'a5'], 'mango': ['g3'], 'grapes': ['a1'], 'pear': ['a2']}
  </code></pre>
  
  <p>Which is exactly what I wanted. However, on a much bigger dictionary, this program would continuously run.
  Is there any way to create a new function with linear time complexity with the same output as I have shown above</p>