title: Replace words in string with words from list using python
body: <p>I'm working on creating a word cloud program in Python and I'm getting stuck on a word replace function. I am trying to replace a set of numbers in an html file (so I'm working with a string) with words from an ordered list. So <code>000</code> would be replaced with the first word in the list, <code>001</code> with the second, etc.</p>
  
  <p>So below I have it selecting the word to replace <code>w</code> properly but I can't get it to properly replace the it with the words from the string. Any help is appreciated. Thanks!</p>
  
  <pre><code>def replace_all():  
    text = '000 001 002 003 '
    word = ['foo', 'bar', 'that', 'these']
    for a in word:    
      y = -1
      for w in text:     
        y = y + 1
        x = "00"+str(y)
        w = {x:a}      
        for i, j in w.iteritems():
          text = text.replace(i, j)
    print text      
  </code></pre>