Reputation: 69
I have written this code to print the names in the list 'magicians'.
def show_magicians(mag_names):
print("Here is the name of the magicians")
print("The old list is :", mag_names)
while mag_names:
full_name = mag_names.pop()
printed_list.append(full_name.title())
print("The name of the magician is ", full_name.title())
print("The old list now :", mag_names)
print("The new list is :", printed_list)
magicians = ['houdini', 'williams', 'sunderland']
printed_list = []
show_magicians(magicians)
My for loop code
for magician in mag_names:
full_name = mag_names.pop()
printed_list.append(full_name.title())
If I execute with while
loop the code works fine with every name printed but with for
loop the first element of the list is not printed as intended.
Upvotes: 0
Views: 137
Reputation: 3711
while
loop does not keep track of the indexes itself as does the for
loop.
while loop
: It just checks that mag_names
is non-empty and it will continue to loop over. Your list becomes empty at one point of time due to popping of elements. And thus you end up the loop. While loop does not iterate to the next element on its own.
for loop
: For loop increments to next element in each of its iteration. In your case you are also popping the element in each element thus list size reduces by 2 in each iteration, thus you dont get the same result as in while loop
The problem with your code is you are using the same list for iteration and for popping the elements. Thus in each iteration the index advances by 1 and at the same time last element is popped out.
The solution would be to use a fresh copy of mag_names
in the iteration process by [:]
def show_magicians(mag_names):
print("Here is the name of the magicians")
print("The old list is :", mag_names)
for magician in mag_names[:]:
full_name = mag_names.pop()
printed_list.append(full_name.title())
print("The name of the magician is ", full_name.title())
print("The old list now :", mag_names)
print("The new list is :", printed_list)
magicians = ['houdini', 'williams', 'sunderland']
printed_list = []
show_magicians(magicians)
Upvotes: 1
Reputation: 1972
Instead writing a complex bit of code, let's make it simple by using append
and then using printed_list[:] = []
to empty the list. Here is the code:
def show_magicians(magicians):
print("Here is the name of the magicians")
print("The old list is :",magicians)
while magicians:
full_name = magicians.pop()
printed_list.append(full_name.title())
print("The name of the magician is ",full_name.title())
print("The old list now :",magicians)
print("The new list is :",printed_list)
print("***********With for loop******************")
for magician in printed_list:
printed_list_new.append(magician)
print("The name of the magician is ", magician)
printed_list[:] = []
print("The old list now:", printed_list)
print("The new list now:", printed_list_new)
magicians = ['houdini', 'williams', 'sunderland']
printed_list = []
printed_list_new = []
show_magicians(magicians)
I tweaked some pieces of your code, to make it work. Hope it helps :)
Upvotes: 0
Reputation: 3447
The way you wrote the for loop is the problem. As for loop takes one element from the list and passes that value inside the block and repeats the same for the next element.
for magician in mag_names:
here magician has one value from the start of the mag_names, in your case it is 'houdini' You shouldn't pop the mag_names inside your for loop again. So the correct code for 'for' loop will be like this
for magician in mag_names:
full_name = magician
printed_list.append(full_name.title())
Your implementation of the while loop works fine because while works differently. It executes the block as long as the condition is true. So the
while mag_names:
will evaluate to True
until the items are empty. And as they are being popped out one by one in your code the list shrinks and eventually reaches empty and evaluates to False
And the reason why for loop and while loop implementation outputs are reversed should be obvious to you now.
Upvotes: 2