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>Okay, you already know how to iterate through the list. You want to make a similar function, where you modify the string referred to my <code>magician</code> to say, "the Great".</p>

<p>So, if you had a string "Smedley" and you wanted to change that to a string "Smedley the Great" how would you do it?</p>

<p><strong>update</strong></p>

<p>So now that the answer's been given away anyway, there are a couple of other options that are more "functional" and arguably safer since you're protected from aliasing etc.</p>

<p>Option 1: create a new list, eg: (These examples done with <a href="https://ipython.org/" rel="nofollow">iPython</a> which is a very handy tool, worth installing for learning Python)</p>

<pre><code>def make_great_1(lst):
    rtn = []
    for m in lst:
        rtn.append(m+" the Great")
    return rtn

In [7]: mages = [ 'Gandalf', 'Mickey' ]

In [8]: make_great_1(mages)
Out[8]: ['Gandalf the Great', 'Mickey the Great']
</code></pre>

<p>Option 2: use a list comprehension:</p>

<pre><code>In [9]: [ mg+" the Great" for mg in mages ]
Out[9]: ['Gandalf the Great', 'Mickey the Great']
</code></pre>

<p>Now, the problem says to <em>modify</em> the list, and it's easy to imagine that means you should modify the strings, but in fact Python (unless you use a <code>MutableString</code>) just makes a copy anyway.  If you want to be really picky, either of these options could be re-done to assign the new list to <code>mages</code> in my case, or <code>magician_names</code> in yours.</p>

python