title: iterating through values in a dictionary to invert key and values
body: <p>I am trying to invert an italian-english dictionary using the code that follows.</p>
  
  <p>Some terms have one translation, while others have multiple possibilities. If an entry has multiple translations I iterate through each word, adding it to english-italian dict (if not already present). </p>
  
  <p>If there is a single translation it should not iterate, but as I have written the code, it does. Also only the last translation in the term with multiple translations is added to the dictionary. I cannot figure out how to rewrite the code to resolve what should be a really simple task </p>
  
  <pre><code>from collections import defaultdict
  
  def invertdict():
      source_dict ={'paramezzale (s.m.)': ['hog', 'keelson', 'inner keel'], 'vento (s.m.)': 'wind'}
  
      english_dict = defaultdict(list)
  
      for parola, words in source_dict.items():
          if len(words) &gt; 1: # more than one translation ?
              for word in words: # if true, iterate through each word
                  word = str(word).strip(' ')
                  print(word)
          else: # only one translation, don't iterate!!
              word = str(words).strip(' ')
              print(word)
          if word in english_dict.keys(): # check to see if the term already exists
              if english_dict[word] != parola: # check that the italian is not present 
                  #english_dict[word] = [english_dict[word], parola]
                  english_dict[word].append(parola).strip('')
          else:
             english_dict[word] = parola.strip(' ')
  
      print(len(english_dict)) 
  
      for key,value in english_dict.items():
         print(key, value)
  </code></pre>
  
  <p>When this code is run, I get :</p>
  
  <pre><code>hog
  keelson
  inner keel
  w
  i
  n
  d
  2
  inner keel paramezzale (s.m.)
  d vento (s.m.)
  </code></pre>
  
  <p>instead of  </p>
  
  <pre><code>hog: paramezzale, keelson: paramezzale, inner keel: paramezzale, wind: vento
  </code></pre>