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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:58:26+00:00 2026-06-14T19:58:26+00:00

I have this function that creates a Ball object and appends it to a

  • 0

I have this function that creates a Ball object and appends it to a list:

list_of_balls = []

def create_new_ball():
    newball = Ball()
    list_of_balls.append(newball)

I need to be able to remove specific instances of Ball from that list. I have no idea how to do this. I cant use list_of_balls.index() because all the instances are attached to the private variable newball. When I get the list something like this is returned:

[<main.Foo instance at 0x22149c>, <main.Foo instance at 0x22111c>, <main.Foo instance at 0x2209b4>, <main.Foo instance at 0x22129c>]

So I have no idea of how to aim specific instances.

Is there a way to remove specific instances of Ball from that list? Each Ball instance needs to be able to remove itself from the list, but how do the instance knows who it is on the list and/or what is its index?

Edit: What I need to do is to identify them on that list (or dictionary, or other alternative). I know how to remove items from a list, what I can’t figure out is how to do it specifically.

I want to do exactly what aList.remove() does, but I don’t have a variable to use as argument, so I need an alternative.

  • 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-14T19:58:27+00:00Added an answer on June 14, 2026 at 7:58 pm

    list.remove seems to work Ok:

    >>> class Ball(object): pass
    ... 
    >>> balls = [ Ball() for _ in range(10) ]
    >>> balls
    [<__main__.Ball object at 0xe5350>, <__main__.Ball object at 0xe52b0>, <__main__.Ball object at 0xe5250>, <__main__.Ball object at 0xe5370>, <__main__.Ball object at 0xe5390>, <__main__.Ball object at 0xe53b0>, <__main__.Ball object at 0xe53d0>, <__main__.Ball object at 0xe5410>, <__main__.Ball object at 0xe5430>, <__main__.Ball object at 0xe5450>]
    >>> balls.remove(balls[4])
    >>> balls
    [<__main__.Ball object at 0xe5350>, <__main__.Ball object at 0xe52b0>, <__main__.Ball object at 0xe5250>, <__main__.Ball object at 0xe5370>, <__main__.Ball object at 0xe53b0>, <__main__.Ball object at 0xe53d0>, <__main__.Ball object at 0xe5410>, <__main__.Ball object at 0xe5430>, <__main__.Ball object at 0xe5450>]
    >>> len(balls)
    9
    

    The real question is how do you know which Ball you want to remove?

    Based on the comments, it looks like you want all of the balls which have bounced N times to be removed:

    to_remove = [x for x in balls if x.times_bounced > N]
    for x in to_remove:
        balls.remove(x)
    

    Alternatively, if you don’t need to do this in place, you can create a new list with balls which haven’t bounced N times:

    balls = [ x for x in balls if x.times_bounced <= N ]
    

    Finally, you can use this idiom and slice assignment to do the whole thing in place if you need to:

    balls[:] = [ x for x in balls if x.times_bounced <= N ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in a Code Igniter model that creates a new video.
I have this jquery function that works, except I need to add something. I
I have this function that creates a unique number for hard-disk and CPU combination.
Say i have this function that dynamically creates my namespace for me when I
I have this working function that finds folders and creates an array. function dua_get_files($path)
I have a function that creates an image with the canvas object. The last
I have a function that creates a binary sha1, but I need a result
I have this code in a function that creates an anchor, stores data and
i have a javascript function that creates inputfields for uploading images. i have this
I have this class that have a function to load other classes and create

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.