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

  • Home
  • SEARCH
  • 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 9068453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:11:49+00:00 2026-06-16T17:11:49+00:00

Every time I run this program, I get this error: ValueError: list.remove(x): x not

  • 0

Every time I run this program, I get this error:

ValueError: list.remove(x): x not in list

I am trying to lower the health of a single alien whenever it is hit by a bolt. That single alien should also be destroyed if its health is <= 0. Similarly, the bolt would also be destroyed. Here is my code:

def manage_collide(bolts, aliens):
    # Check if a bolt collides with any alien(s)
    for b in bolts:
        for a in aliens:
            if b['rect'].colliderect(a['rect']):
                for a in aliens:
                    a['health'] -= 1
                    bolts.remove(b)
                    if a['health'] == 0:
                        aliens.remove(a)
    # Return bolts, aliens dictionaries
    return bolts, aliens

The ValueError happens on the line aliens.remove(a). Just to clarify, both the aliens and bolts are lists of dictionaries.

What am I doing wrong?

  • 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-16T17:11:50+00:00Added an answer on June 16, 2026 at 5:11 pm

    You should not remove items from a list you are looping over. Create a copy instead:

    for a in aliens[:]:
    

    and

    for b in bolts[:]:
    

    Modifying a list while looping over it, affects the loop:

    >>> lst = [1, 2, 3]
    >>> for i in lst:
    ...     print i
    ...     lst.remove(i)
    ... 
    1
    3
    >>> lst
    [2]
    

    Removing items from a list you are looping over twice makes things a little more complicated still, resulting in a ValueError:

    >>> lst = [1, 2, 3]
    >>> for i in lst:
    ...     for a in lst:
    ...         print i, a, lst
    ...         lst.remove(i)
    ... 
    1 1 [1, 2, 3]
    1 3 [2, 3]
    Traceback (most recent call last):
      File "<stdin>", line 4, in <module>
    ValueError: list.remove(x): x not in list
    

    When creating a copy of the lists you are modifying at each level of your loops, you avoid the problem:

    >>> lst = [1, 2, 3]
    >>> for i in lst[:]:
    ...     for i in lst[:]:
    ...         print i, lst
    ...         lst.remove(i)
    ... 
    1 [1, 2, 3]
    2 [2, 3]
    3 [3]
    

    When you have a collision, you only need to remove the b bolt once, not in the loop where you hurt the aliens. Clean out the aliens separately later:

    def manage_collide(bolts, aliens):
        for b in bolts[:]:
            for a in aliens:
                if b['rect'].colliderect(a['rect']) and a['health'] > 0:
                    bolts.remove(b)
                    for a in aliens:
                        a['health'] -= 1
        for a in aliens[:]:
            if a['health'] <= 0:
                aliens.remove(a)
        return bolts, aliens
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get text from a text field but every time I run
Everytime I run the program, this mysterious error pops up saying that I have
I have this warning every time I run my CGI-script (output is rendered by
I am getting a warning I do not fully understand every time I run
No sure why, but every time I run my OpenGL program on my home
Every time the program terminates when it runs the third time through this loop:
Every time I run the code below it is supposed to come up with
I'm running through the gitimmersion.com labs and every time I run a: git hist
I have a spork gem issue. Every time I run the spork command I
I am having problem with RubyMine 4.0.1 on Windows 7. Every time i run

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.