title: Replacing element in list without list comprehension, slicing or using [ ]s
body: <p><a href="http://cscircles.cemc.uwaterloo.ca/14-methods/" rel="nofollow noreferrer">I'm taking this online Python course</a> and they do not like the students using one-line solutions. The course will not accept brackets for this solution.</p>
  
  <p>I already solved the problem using list comprehension, but the course rejected my answer.</p>
  
  <p>The problem reads:</p>
  
  <blockquote>
    <p>Using <code>index</code> and other list methods, write a function <code>replace(list, X, Y)</code> which replaces all occurrences of <code>X</code> in <code>list</code> with <code>Y</code>. For example, if <code>L = [3, 1, 4, 1, 5, 9]</code> then <code>replace(L, 1, 7)</code> would change the contents of <code>L</code> to <code>[3, 7, 4, 7, 5, 9]</code>. To make this exercise a challenge, you are not allowed to use <code>[]</code>.</p>
    
    <p>Note: you don't need to use return.</p>
  </blockquote>
  
  <p>This is what I have so far, but it breaks because of TypeError: 'int' object is not iterable.</p>
  
  <pre><code>list = [3, 1, 4, 1, 5, 9]
  
  def replace(list, X, Y):
     while X in list:
        for i,v in range(len(list)):
           if v==1:
              list.remove(1)
              list.insert(i, 7)
  
  replace(list, 1, 7)
  </code></pre>
  
  <p>This was my original answer, but it was rejected.</p>
  
  <pre><code>list = [3, 1, 4, 1, 5, 9]
  
  def replace(list, X, Y):
     print([Y if v == X else v for v in list])
  
  replace(list, 1, 7)
  </code></pre>
  
  <p>Any ideas on how to fix my longer solution?</p>