AndreiVataselu
AndreiVataselu

Reputation: 234

Trying to modify a list in Python

I'm reading the Python Crash Course book by Eric Matthes, and I'm working on this exercise:

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 .

8-10. Great Magicians: Start with a copy of your program from Exercise 8-9 . Write a function called make_great() that modifies the list of magicians by add- ing the phrase the Great to each magician’s name . Call show_magicians() to see that the list has actually been modified .

In his book he modifies lists by passing them as arguments in a function, and that function changes the list, but all I get is a list of the original names. What did I do wrong?

def show_magicians(magicians):
    for magician in magicians:
        print(magician.title())


def make_great(magicians):
    for magician in magicians:
        magician = "Great " + magician



magicians = ['hermione','mihai','harry potter', 'voldemort']

make_great(magicians)
show_magicians(magicians)

Upvotes: 0

Views: 300

Answers (3)

Carcigenicate
Carcigenicate

Reputation: 45826

You can't reassign an element into the list like that. When you reassign magician in the loop, all that's doing is reassigning the local variable that used to refer to a String in magicians.

Use a comprehension to create a new list that has "Great" prepended to each string; which you return:

def make_great(magicians):
  return ["Great " + magician for magician in magicians] 

magicians = ['hermione','mihai','harry potter', 'voldemort']

# Returned a new list to use
great_magicians = make_great(magicians)
print(great_magicians) # ['Great hermione', 'Great mihai', 'Great harry potter', 'Great voldemort'] 

You can alter lists passed in to functions:

def f(my_list):
  my_list[0] = 2

l = [1, 2, 3]
f(l)
print(l) # [2, 2, 3]

And you can mutate the objects in the list while iterating:

def f(my_list):
  for l in my_list:
    l.append(9)

l = [[], []]
f(l)
print(l) # [[9], [9]]

You just can't do this with immutable objects like Strings, since immutable objects can't themselves be altered in any way.

Upvotes: 4

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15606

As answered, your function did not modify orIn Python 3.6+, you could simplify the function using f-formatting

magicians = ['hermione','mihai','harry potter', 'voldemort']

def make_great(magicians):
    return [f'Great {i.title()}' for i in magicians]


print(make_great(magicians))

Upvotes: 0

Prune
Prune

Reputation: 77910

Your make_great function does nothing to the list.

def make_great(magicians):
    for magician in magicians:
        magician = "Great " + magician

It has one local variable that you change several times, but you never change the list. Instead:

def make_great(magicians):
    for i in range(len(magicians)):
        magicians[i] = "Great " + magicians[i]

Output:

Great Hermione
Great Mihai
Great Harry Potter
Great Voldemort

Upvotes: 3

Related Questions