Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:51:38+00:00 2026-06-15T02:51:38+00:00

Winkleson here looking for some help on a very simplistic question… I’m pretty out

  • 0

Winkleson here looking for some help on a very simplistic question… I’m pretty out of it at the moment but I’d like to figure out what I am doing wrong with this problem 🙂 Of course anyother methods to solve the problem would be excellent! Thankyou in advance!

Question:


Remove Item

Create a function that takes a list and a value and returns a list
with all occurrences of the given value removed.

Pretty simple right? My head is going to hurt when I facepalm… Anyways here are the calls.

Calls:


>>> remove(['a','b','c','d','e'],'e') 
['a','b','c','d'] 
>>> remove([4,2,7,6,7,8,3,1,3,5],3) 
[4,2,7,6,7,8,1,5] 
>>> remove([4,4,4,4],4) 
[] 
>>> remove([1,2,3,4,5,6,7],'hi') 
[1,2,3,4,5,6,7]

My Code:


def remove(l,o): #l is list, o is object
    for i in l:
        if i == o: #If the current item is the object to be removed...
            l.remove(o) #Remove the object      
    return l #Finally return the list.

So… Here’s the issue:


Call: remove([4,4,4,4],4)

Supposed to return:[]

What is returned: [4, 4]

Correct: False


So… If anyone out there knows what’s going on it would be great if you were to share your knowledge! Furthermore any other methods of solving would be great as well. Hints would also be great if they aren’t too vague. Anyways, Thanks in advance! – Winkleson

P.s. I’m still a beginner programmer so please don’t be too hard on me 😛 Thanks alot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:51:39+00:00Added an answer on June 15, 2026 at 2:51 am

    Your problem is that you’re trying to iterate over the list at the same time you’re changing the list. In the first iteration:

    l = [4, 4, 4, 4]
    

    Python looks at l[0], and deletes it.

    Now the list is:

    l = [4, 4, 4]
    

    Since it just did l[0], Python now wants to look at l[1]. But since the list has changed, the value that used to be at l[1] is at l[0] instead, and gets skipped.

    One option is to build a new list as you go:

    def remove(l,o):  
        new_list = []  
        for i in l:        
            if i != o:
                new_list.append(i)       
        return new_list
    

    Since this is a common sort of operation, Python lets you do the same thing with “list comprehensions”, which are very handy:

    def remove(l,o)
        new_list = [item for item in l if item != o]
    

    (As a side note, it’s generally a bad idea to use 1-letter variable names. It’s an especially bad idea with “o” and “l”, which can easily be mistaken for “0” and “1”, respectively.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Winkleson here with another question for a http://singpath.appspot.com problemset question. I am a beginner
Winkleson here! I am currently learning Python when I got stuck on a problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.