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:
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)
id(reversed(x)) 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 |