Python Forum
I don't understand why this doesn't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't understand why this doesn't work
#1
can someone please explain to me why the third item in the list is not removed?

def alist(x):
    for i in x:
        if len(i) != 4:
            x.remove(i)
    return x


test = alist(['Yoda', '123', 'whyisthisnotremoved', 'testing', 'test', '', '1234'])
print(test)
Thanks

oh.. as I am looking at it:
Is it because when the first item is removed the index changes and moves the 3d item to index [1] in this case, while the for loop continues on with index 2 etc.. ?

I guess that might be it now that I think about it, but I would still like to hear the explanation from someone more experienced that my self. Because I could not figure it out otherwise.

If thats the case what would be a good solution for this to work as I intended?
Reply
#2
(Dec-19-2019, 07:10 PM)sandeen Wrote: Is it because when the first item is removed the index changes and moves the 3d item to index [1] in this case, while the for loop continues on with index 2 etc.. ?
that's correct.
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, may I ask what could be a good solution for this to work as I intended?
Reply
#4
def alist(x):
    return [item for item in x if len(item) == 4]

def alist2(x):
    return list(filter(lambda item: len(item) == 4, x))
 
test = alist(['Yoda', '123', 'whyisthisnotremoved', 'testing', 'test', '', '1234'])
test2 = alist2(['Yoda', '123', 'whyisthisnotremoved', 'testing', 'test', '', '1234'])
print(test)
print(test2)
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
(Dec-19-2019, 07:36 PM)buran Wrote:
def alist(x): return [item for item in x if len(item) == 4] def alist2(x): return list(filter(lambda item: len(item) == 4, x)) test = alist(['Yoda', '123', 'whyisthisnotremoved', 'testing', 'test', '', '1234']) test2 = alist2(['Yoda', '123', 'whyisthisnotremoved', 'testing', 'test', '', '1234']) print(test) print(test2)

Thanks again, simpel and easy to read.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 940 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,792 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 889 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,715 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,206 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,014 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,707 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,624 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,198 Aug-10-2021, 08:10 AM
Last Post: snippsat
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,957 Aug-10-2021, 07:00 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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