What's up with this code?

0 Robin Cox · October 20, 2014

list1 = ["Me", "Jean-Pierre", "Beverley", "Philip", "Louise", "Charmaine"]
for name in list1:
    if name is "Me":
        print("Got You!")
        list1.remove(name)
    else:
        print("Hello",name+", how are you?")


ok so the code itself is pretty simple and I was just messing around with lists, when I encountered a problem with it, whenever I remove my name from the list i.e "me" it also removes the entry "Jean-Pierre". I don't understand this at all?

I've tried different methods of removing my name such as

list1.pop(list1.index("me"))

but it doesn't help. What did resolve the issue was sorting the list before the for loop using:


list1.sort()

Can anybody explain this?

Post a Reply

Replies

Oldest  Newest  Rating
0 Brock Cryoston · October 21, 2014
So I did some tests, and here's what I've got. 
http://i.imgur.com/KgL8IgO.png
The list1.remove(num) doesn't remove the same variable as the print() prints. I don't think this is how it should behave, I think this is a bug.
What could explain this is that the variable remove function is offset by 1. So when the for loop starts at 0, it will kill 1, and as it goes to the next, 1 is not there so it prints 2, but kills 3 and so on.
+1 Colonel Panic · October 23, 2014
Works perfect for me.  Can you show some output?

[scochran@Exicutioner:~ ] $ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = ["Me", "Jean-Pierre", "Beverley", "Philip", "Louise", "Charmaine"]
>>> for name in list1:
...     if name is "Me":
...         print("Got You!")
...         list1.remove(name)
...     else:
...         print("Hello",name+", how are you?")
...
Got You!
('Hello', 'Beverley, how are you?')
('Hello', 'Philip, how are you?')
('Hello', 'Louise, how are you?')
('Hello', 'Charmaine, how are you?')
>>> print list1
['Jean-Pierre', 'Beverley', 'Philip', 'Louise', 'Charmaine']
0 Robin Cox · October 24, 2014
When the code runs it's only supposed to delete me from the list, as you can see in the output it skips Jean-Pierre too
0 Robin Cox · October 24, 2014
Oh, and hey brock, but if what you're saying is true, then why does the remove methods delete only me from the list and not Jean-Pierre? Sure it doesn't show 'me' and 'Jean-Pierre' however, I've been successfully removed and he hasn't, so if it was offset by I shouldn't be removed at all seeing as I'm at index 0
+1 Colonel Panic · October 26, 2014
No Robin, I cannot see in your output, you have not provided output.  Just the script itself, and when It works perfectly as you would expect.  

Note that you are not printing the value of your list at the end of your code.  
+1 Colonel Panic · October 26, 2014


The list1.remove(num) doesn't remove the same variable as the print() prints. I don't think this is how it should behave, I think this is a bug.

What could explain this is that the variable remove function is offset by 1. So when the for loop starts at 0, it will kill 1, and as it goes to the next, 1 is not there so it prints 2, but kills 3 and so on


This is not a bug, it is behaving exactly as it should.  The remove function removes the first occurrence in the list that is == the value of that parameter that is passed into it. 




scochran@Millennium-Falcon:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [1,2,1,3,1,4,1,5]
>>> list1.remove(1)
>>> print list1
[2, 1, 3, 1, 4, 1, 5]
>>> list1.remove(1)
>>> print list1
[2, 3, 1, 4, 1, 5]


+2 Doug Fresh · November 10, 2014
The issue here is actually with the for loop. When you remove the value from a list, all the other values shift places minus one. In other words, what was in position 1, is now in position 0, what was in position 2 is now in position 1. The for loop continues until it reaches it's designated stop value. 
That's why if you take out the list1.remove(name), the program runs and shows all the names you requested. 
The way to fix this so that you can remove a name and iterate through all the names is this:



## Made is Python 3.4 ##

list1 = [ "Me", "Jean-Pierre","Beverley", "Philip", "Louise", "Charmaine"]

nameSpot = 0
for count in range(len(list1)):
    name = list1[nameSpot]
    if name == "Me":
        print("Got You!")
        list1.remove(name)
               
    else:
        print("Hello", name, ", how are you?")
        nameSpot += 1


Not the prettiest code after 5 minutes, but this will work.


By iterating through the number of names, rather than the list itself, you avoid the issues of removing values from the list. This also allows for multiple occurrences of "Me" throughout the list. 

If you are using Python 2.7, you may need to concatenate your strings in the print statements, but as of Python 3, this is no longer needed unless returning a value from a class. So comma separation works just fine. 
0 Robin Cox · May 9, 2015
Thanks. I appreciate all the feedback. As for not providing my output i mean't in your own output jean-pierre is not displayed, only when you print the list itself.
0 Pavel Dimi · May 21, 2015
list1 = ["Me", "Jean-Pierre", "Beverley", "Philip", "Louise", "Charmaine"]
list1_copy = list1[:]
for name in list1_copy:
    if name is "Me":
        print("Got You!")
        list1.remove(name)
    else:
        print("Hello",name+", how are you?")
  • 1

Python

106,976 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator