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 9110529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:17:29+00:00 2026-06-17T03:17:29+00:00

I wrote a function to remove odd number from a list, like that: def

  • 0

I wrote a function to remove odd number from a list, like that:

def remove_odd(l):
    for i in l:
        if i % 2 != 0:
            l.remove(i)
    print l
    return l

remove_odd([4,5,4])
remove_odd([4,5,4,7,9,11])
remove_odd([4,5,4,7,9,11,12,13])

It returns:

[4, 4]
[4, 4, 9]
[4, 4, 9, 12]

-> wrong

but when I change to remove even number:

def remove_even(l):
    for i in l:
        if i % 2 == 0:
            l.remove(i)
    print l
    return l

remove_even([4,5,4])
remove_even([4,5,4,7,9,11])
remove_even([4,5,4,7,9,11,12,13])

The answer is OK:

[5]
[5, 7, 9, 11]
[5, 7, 9, 11, 13]

What is wrong with the remove_odd() func?
I know people usually create the second list inside the func then append even number to that list, but can we solve this exercise with list.remove() ?

Thank you!

  • 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-17T03:17:30+00:00Added an answer on June 17, 2026 at 3:17 am

    Your function is working in another way than you would expect. The for loop takes first element, than second etc., so when you remove one element, others change their positions and can be skipped by it (and that happens in your case) when they are preceded by another odd number.

    If you insist on using .remove() method, you must operate on a copy instead, like this:

    def remove_odd(1):
        for i in l[:]:
            if i % 2 != 0:
                l.remove(i)
        return l
    

    (l[:] is a shallow copy of list l)

    However, I think using list comprehension would be much clearer:

    def remove_odd(l):
        return [x for x in l if x % 2 == 0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to write generic function that remove the duplicate elements from array. public
I wrote a function in c++ to remove parenthesis from a string, but it
I wrote a function in Java to fetch 10 links by their IDs from
suppose i wrote my javascript in my aspx page like below <script type=text/javascript> function
I wrote a function to check whether a number is prime or not: prime
I wrote a function that reads an unknown length string until Enter is pressed
I wrote the following function for removing duplicate characters from a string..For ex: if
I wrote the Ajax function like below. It is not working properly. If I
I'm learning function templates in C++, so I wrote a simple function to remove
I have a function I wrote that gets the height of the application without

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.