Python crash course 8-10
<p>I'm currently reading Python Crash Course by Eric Matthes and doing some of the problem sets. One of them is giving me some difficulty and I was hoping someone could help me with it. </p>

<blockquote>
  <p>8-9. Magicians: Make a list of magician's names. Pass the list to a function called show_magicians(), which prints the name of each magician in the list.</p>
  
  <p>8-10. Great Magicians: Start with a copy of your program from exercise 8-9. Write a function make_great() that modifies the list of magicians by adding the phrase <em>the great</em> to each magician's name. Call show_magicians() to see that the list has actually been modified. </p>
</blockquote>

<p>And this is what I have for 8-9:</p>

<pre><code>magician_names=['Alice', 'Alex', 'Blake']
def show_magicians(n):
    for magician in n:
        print (magician)

show_magicians(magician_names)
</code></pre>

<p>I just don't really know what to do for 8-10</p>

<p>The question asks you to modify <code>magician_names</code>. Since each entry in <code>magician_names</code> is a <code>str</code>, and <code>strs</code> are unmodifiable, you cannot simply modify each <code>str</code> in <code>magician_names</code> by appending <code>" the great"</code> to it. Instead, you need to <em>replace</em> each <code>str</code> in <code>magician_names</code> with its modified version.</p>

<p>So this seemingly straightforward approach:</p>

<pre><code>def make_great(l):
    for name in l:
        name += " the great"

make_great(magician_names)
</code></pre>

<p>does not modify <code>magician_names</code>. It simply creates a new <code>str</code> object and assigns that to the <code>name</code> variable local to the <code>for loop</code>. Each element of <code>magician_names</code> is unchanged, i.e. it still points to the same <code>str</code> object.</p>

<p>But this approach:</p>

<pre><code>def make_great(l):
    for index, name in enumerate(l):
        l[index] = name + " the great"

make_great(magician_names)
</code></pre>

<p>changes the <code>str</code> objects pointed to by <code>magician_names</code> and hence modifies <code>magician_names</code> as required.</p>

<p><hr>
To your comment, </p>

<pre><code>for index, name in enumerate(l):
    l[index] = name + " the great"
</code></pre>

<p>is just the Pythonic way to write:</p>

<pre><code>for index in range(len(l)):
    name = l[index]
    l[index] = name + " the great"
</code></pre>

<p><code>enumerate(l)</code> returns an iterable (actually a lazy generator) that yields a matched <code>index, element</code> pair for each element in <code>l</code>.</p>

python