Python Forum
Question about for loop not creating an infinite loop.
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.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using a For Loop to subtract numbers from an input function. Anunderling 9 2,655 Sep-22-2025, 08:56 PM
Last Post: deanhystad
  Problem with For loop indentation Mormolyce 10 2,984 Sep-03-2025, 03:55 AM
Last Post: DeaD_EyE
  Break within an if statement inside a while loop not working Python_more_on 2 1,839 Sep-03-2025, 12:55 AM
Last Post: Pedroski55
  Error loop with Chatgpt sportak12 1 3,340 Aug-26-2025, 08:11 AM
Last Post: python_developer
  Returning data on button click by buttons created by a loop bradells 3 1,273 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 4,516 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 2,590 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 1,516 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 1,321 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  How to convert while loop to for loop in my code? tatahuft 4 1,618 Dec-21-2024, 07:59 AM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.