Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Question about for loop not creating an infinite loop.
#1
Hi, I'm new to programming with python and I completed one of the exercises of the book I use to learn but I don't understand why this code doesn't make an infinite loop of printing the list. The problem is: Write a function called make_great() that modifies the list of magicians by adding the phrase the Great to each magician’s name. Matthes, Eric. Python Crash Course: A Hands-On, Project-Based Introduction to Programming (p. 150). No Starch Press. Kindle Edition.


magicians = ['xerath', 'ryze', 'sylas', 'vladimir']


def make_great(list_of_magicians):
    for magician in list_of_magicians:
        person = list_of_magicians.pop()
        add_text = 'the great ' + person
        list_of_magicians.insert(0, add_text)
        print(list_of_magicians)


make_great(magicians)
This is the output:
Output:
['the great vladimir', 'xerath', 'ryze', 'sylas'] ['the great sylas', 'the great vladimir', 'xerath', 'ryze'] ['the great ryze', 'the great sylas', 'the great vladimir', 'xerath'] ['the great xerath', 'the great ryze', 'the great sylas', 'the great vladimir']
I have tried another solution to this problem by doing this:
magicians = ['xerath', 'ryze', 'sylas', 'vladimir']


def make_great(list_of_magicians):
    for magician in list_of_magicians:
        add_text = 'the great ' + magician
        list_of_magicians.insert(0, add_text)
        print(list_of_magicians)


make_great(magicians)
And this is the output:
Output:
['the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] ['the great xerath', 'the great xerath', 'the great xerath', 'the great xerath', 'the great xerath', 'xerath', 'ryze', 'sylas', 'vladimir'] -This continues by adding one more element to the list each time.-
Can someone explain to me why this two solutions make different outputs? Thanks ! :D.
Quote
#2
The pop method removes the last item from the list, as well as returning that item. So the first loop shrinks the list by one before growing it by one, thus keeping it the same size. The second one has no pop, so items are not removed (iterating with a for loop does not remove items from the list).

Note that modifying a list while you are iterating over it is a bad idea. It works out in the code above, but it doesn't always. A better way to do what your exercise wants is to build a new list:

great_magicians = []
for magician in magicians:
    the_great = 'the great {}'.format(magician)
    great_magicians.append(the_great)
    print(great_magicians)
The format method of strings is recommended over string addition for efficiency reasons. In 3.6 or later, look into f-strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  simple key value loop help siten0308 4 153 Jun-25-2019, 02:53 PM
Last Post: siten0308
  Simple while loop only works on first attempt jsb83 2 83 Jun-20-2019, 08:57 PM
Last Post: jsb83
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 99 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  Arrange list of tuple using loop batchenr 7 213 Jun-16-2019, 03:24 PM
Last Post: Abdullah
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 162 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Loop through array items dynamically in a certain format bhojendra 3 130 Jun-11-2019, 03:37 AM
Last Post: micseydel
  change to loop amirliker 3 148 Jun-06-2019, 01:08 PM
Last Post: heiner55
  while loop ef26 5 194 May-27-2019, 01:25 PM
Last Post: DeaD_EyE
  my function is stuck on loop - even when it not supposed to be korenron 2 221 May-26-2019, 12:31 PM
Last Post: korenron
  Working on my FOR loop wilsonrivas 1 309 May-24-2019, 04:05 PM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)