Python Forum
remove elements method not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove elements method not working
#1
Hello,
I am trying to use remove() to remove elements from the list but it is not happening. Kindly help.

l = [10, 10, 10, 10, 20, 20, 30]
x = int(input("Enter element to remove "))
for i in l:
    if i == x:
        l.remove(i)
print(l)
Output:
Enter element to remove 10 [10, 10, 20, 20, 30]
Reply
#2
never change list while iterating over it. Iterate over copy of the list
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you but I didn't understand much. I am new to python. Don't mind.
Reply
#4
Maybe this will make it a bit clear.
spam = ['a', 'b', 'c', 'd']

for char in spam:
    print(f'current char:{char}')
    if char in 'ab':
        spam.remove(char)
    print(f'current state of list: {spam}')
Output:
current char:a current state of list: ['b', 'c', 'd'] current char:c current state of list: ['b', 'c', 'd'] current char:d current state of list: ['b', 'c', 'd']
It's similar with what you do, but with chars, to make it more clear. Based on your logic, it should remove chars 'a' and 'b'.
but after removing 'a', now second element in list is already 'c', so it never visit 'b'.

what to do
spam = ['a', 'b', 'c', 'd']

for char in spam[::]: # iterate over a copy of the list
    print(f'current char:{char}')
    if char in 'ab':
        spam.remove(char)
    print(f'current state of list: {spam}')
Output:
current char:a current state of list: ['b', 'c', 'd'] current char:b current state of list: ['c', 'd'] current char:c current state of list: ['c', 'd'] current char:d current state of list: ['c', 'd']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Try this:
numbers = [1, 2, 3, 4, 5, 6, 7]
for i in numbers:
    numbers.remove(i)
print(numbers)
Why is every other number removed? Think about how a for loop might be indexing through a list and what happens tot he list when you remove an item. I use pencil and paper when trying to understand this stuff. Adding lots of print commands can be helpful too.

If someone tells you "never change list while iterating over it. Iterate over copy of the list" you should not accept as truth and try to determine why. Then you'll no longer be able to claim "I didn't understand much."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 986 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  Custom method to handle exceptions not working as expected gradlon93 3 945 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  method to remove zero digits from fraction in decimal Skaperen 17 2,576 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,014 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,550 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Remove specific elements from list with a pattern Xalagy 3 2,623 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  python os.popen is not working for wait method elenaflorence87 0 1,969 Jul-22-2020, 12:56 PM
Last Post: elenaflorence87
  Remove elements from lists leemao 4 2,310 Jun-21-2020, 11:08 AM
Last Post: leemao
  os.remove is not working solomon 1 2,153 Apr-04-2020, 06:28 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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