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
  Possible to use asyncio as a faster while loop? JackMack118 1 121 Dec-03-2019, 09:55 PM
Last Post: JackMack118
  MySql - Loop - CHeck File gcclinux 1 78 Nov-30-2019, 11:51 AM
Last Post: ThomasL
  Is it possible to avoid 2 loops inside another loop? SvetlanaofVodianova 2 174 Nov-27-2019, 02:30 PM
Last Post: Gribouillis
  Using loop for getting data in several List quyenca 4 181 Nov-26-2019, 04:36 PM
Last Post: quyenca
  number repeating twice in loop JonnyEnglish 3 113 Nov-24-2019, 09:23 AM
Last Post: ThomasL
  While loop doesn't end when False Kanashi 2 112 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  Running function from parent module which has a loop in it. ta2909i 1 76 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  For loop in my __init__ doesn't work as expected Jessy 2 95 Nov-18-2019, 10:07 AM
Last Post: buran
  Output in for LOOP iterated Renym 1 81 Nov-17-2019, 09:15 PM
Last Post: perfringo
  while loop Blob 9 208 Nov-15-2019, 03:42 PM
Last Post: the_ophidian_order

Forum Jump:


Users browsing this thread: 1 Guest(s)