Remove function

how to use it?

Page 1 of 1

1 Replies - 2599 Views - Last Post: 18 November 2009 - 07:10 PM Rate Topic: -----

#1 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Remove function

Posted 14 November 2009 - 12:46 PM

So, I tried looking up handy functions for python I could use, thought Remove sounded good, but it says the item can't be found. I don't want it to do anything when the item can't be found... How do I do this?

for i in range(0,dy):
			 number=list[dx][i]
			 numSelect.remove(number)


So, basically, I have this list (numSelect), [1,2,3,4], and I have this other list (list) with any of the numbers from 0 to 4. For however many numbers I have in list, I want it look at each number, and delete that number from numSelect (except 0, because numSelect doesn't have that number).

Is This A Good Question/Topic? 0
  • +

Replies To: Remove function

#2 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 15
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Remove function

Posted 18 November 2009 - 07:10 PM

If you check the python help function (or documentation) for info on remove, you'll get:

Quote

>>> help([].remove)
Help on built-in function remove:

remove(...)
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.


It should be relatively easy to handle the case where the value you want to remove is not in the list.

Thus, you should check that list[dx][i] actually returns a number for the return method to find in your list. It's hard to help more unless you provide the error messages you're getting though.

However, before you do that, you should know that 'list' is a reserved word in python, meaning you cannot make it the name of anything, including a list (as you appear to have done here).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1