Python Forum
question about list and remove function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about list and remove function
#1
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.
Reply
#2
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 = []
Reply
#3
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"]
Reply
#4
[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.
Reply
#5
<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.
Reply
#6
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 420 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Question on dir() function Soorya25 1 1,136 Jan-16-2023, 09:33 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,318 Nov-13-2022, 01:27 AM
Last Post: menator01
  Function not scriptable: Noob question kaega2 3 1,171 Aug-21-2022, 04:37 PM
Last Post: kaega2
  input function question barryjo 12 2,703 Jan-18-2022, 12:11 AM
Last Post: barryjo
  Remove empty keys in a python list python_student 7 3,009 Jan-12-2022, 10:23 PM
Last Post: python_student
  Remove an item from a list contained in another item in python CompleteNewb 19 5,661 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Question on None function in a machine learning algorithm Livingstone1337 1 2,353 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  .remove() from a list - request for explanation InputOutput007 3 2,211 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Remove double quotes from the list ? PythonDev 22 8,745 Nov-05-2020, 04:53 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020