0

I want to write a function that will remove a certain integer from a list. Both of the values are input. For example, removeValues([1, 2, 3], 3) would return the list [1, 2]:

def removeValues(aList, n):
    newList = []
    if n in aList:
        aList.remove(n)

        newList.append(aList)
    else:
        return False

I am not sure if .remove is the right way to go.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
wahlysadventures
  • 139
  • 1
  • 1
  • 8
  • But there is already a method `list.remove` for that ... – wim Oct 21 '15 at 16:53
  • Why are you appending your list to another list ? `[[1,2]]` Just remove it and presto ! `[1,2]` – Onilol Oct 21 '15 at 16:53
  • 1
    *"I am not sure if .remove is the right way to go"* - have you tested it? Did it work? – jonrsharpe Oct 21 '15 at 16:56
  • I tested the list.remove but it has an error. builtins.TypeError: descriptor 'remove' requires a 'list' object but received a 'int' So its saying it got an int and wanted a list. But the thing I want removed is an int....? – wahlysadventures Oct 21 '15 at 16:59
  • 2
    Possible duplicate of [Remove all occurences of a value from a Python list](http://stackoverflow.com/questions/1157106/remove-all-occurences-of-a-value-from-a-python-list) – jlnabais Oct 21 '15 at 16:59
  • Are you trying to remove a single occurrence of that value, or every occurrence? Does `removeValues([1, 2, 3, 3], 3)` return `[1, 2]` or `[1, 2, 3]`? – zayora Oct 21 '15 at 17:01
  • @jlnabais I looked there and all examples are not with user input. There is just a given list in the function that is then messed with. Does it not matter if the list is user input or not? – wahlysadventures Oct 21 '15 at 17:01
  • @zayora it should remove all the values in the list. So it should return [1,2] – wahlysadventures Oct 21 '15 at 17:01
  • @wahlysadventures you can get the list from input and then delete all the occurrences of a given value. Take a look [here](http://stackoverflow.com/questions/14177582/number-numbers-to-list-by-using-input) for how to input a list. Hope this helps. – jlnabais Oct 21 '15 at 17:04

3 Answers3

2

Use a list comprehension

def removeValues(aList, n):
    return [ i for i in aList if i != n ]
Chad S.
  • 6,252
  • 15
  • 25
0
list(filter(lambda x : x != 3, [1,2,3]))

Use filter, Filter takes a function reference and list of elements. the function is written to accept a list element and return true or false based on the desired predicate like x != 3. for every element the function checks the predicate and only if the condition return True the element is included in the output list.

As said in the comments, a simple remove on the list will do the job.

saikumarm
  • 1,565
  • 1
  • 15
  • 30
0

A function that will remove all occurrences of a specific value from a list could be written like so:

>>> def removeAll(list, value):
...     while value in list:
...         list.remove(value)
...
>>> a = [1,2,3,3,4]
>>> removeAll(a, 3)
>>> print( a )
[1,2,4]
zayora
  • 418
  • 2
  • 12