Python Forum
question about list and remove function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: question about list and remove function (/thread-9262.html)



question about list and remove function - wmw12344 - Mar-29-2018

hello,
I am a beginner in programming,started learning python a few days ago.When I was doing exercise,found a problem that I couldn't solve by myself.Could anyone help me about it?Thank you very much.
My python version is 3.6.3
the codes:
name = ["aa", "bb", "cc", "dd"]
for zzz in (name):
    name.remove(zzz)
print(name)
I wanted to delete or remove all values in the list,but the results always contain the values on the even places (like "bb" and "dd").
However,when I execute codes below:
name = ["aa","bb","cc","dd"]
for zzz in (name):
	print("hello, " + zzz)
I can get the results I want,printing every string with all the names in the list.
Is it because this function can not use with remove function?thanks.


RE: question about list and remove function - Larz60+ - Mar-30-2018

the reason is that your indexes get confused once you remove an item from the lsit
If you do it in reverse order, it will work:
name = ["aa","bb","cc","dd"]
for zzz in reversed(name):
    name.remove(zzz)
print(name)
But if all you're trying to do is clear the list, just use:
name = []



RE: question about list and remove function - scidam - Mar-30-2018

Hi,
It is a bad practice to change an iterable object during iterating over it.

When you removing elements from the list, the loop is executed as follows:


Output:
name = ["aa", "bb", "cc", "dd"] # ===== enter the loop (1) for .. in statements gets next value from the list using next(name); so zzz = "aa" (1) remove "aa" from name var. (now name = ["bb", "cc", "dd"]) (2) on the second lap of the for loop: next(name) yields to "cc", because name was changed! so, zzz = "cc" (2) remove "cc" from name; (name = ["bb", "dd"]) (3) trying to get next element next(name) raise (internally) StopIteration, because there are no third element in the array; Here, the loop is completed... ... therefore, name = ["bb", "dd"]



RE: question about list and remove function - wmw12344 - Mar-30-2018

[quote="Larz60+" pid="43394" dateline="1522373388"]

Thank you for the answer,but why does it work on reversed list but not on the original one?
Because according to <Scidam>'s explaination,it shouldn't work on the reversed list neither.
I am not trying to clear the list,I just want to try out the remove function.



[quote="scidam" pid="43395" dateline="1522374285"]
Thank you for the explanation,it really help me a lot.
Could you explain why <Larz60+ >'s answer work,but not the original one? what is the difference between reversed one and the old one?Thanks.


RE: question about list and remove function - scidam - Mar-30-2018

<reversed> creates another Python object:

x = [1,2,3]
id(x)
Output:
139773763888984
id(reversed(x))
Output:
139773763898000
reversed(x) and x have different ids, i.e. they are different objects...

When you iterate over reversed list, e.g. < for zzz in reversed()...>, <reversed(name)> -- a new iterable object, is created when name = ["aa", "bb", "cc", "dd"]; it isn't changed during loop execution.


RE: question about list and remove function - ljmetzger - Mar-30-2018

Quote:I wanted to delete or remove all values in the list,but the results always contain the values on the even places (like "bb" and "dd").

When moving through the list, you are thinking about the values, but Python is thinking about the position in the list. When you iterate in reverse and delete items, the position in the list does not change. When iterating and deleting from the front, after each deletion, the position moves forward leaving you with every other item not deleted (i.e. when deleting the 2nd item, you are really skipping the original 2nd item which is now the 1st item, and deleting the original 3rd item, which is now the 2nd item).

Lewis