l[:] = ( i for i in l if i != "Bob" )
Just as you can use the slice operator for getting a slice, x = y[2:5]
, you can use the slice assignment for setting a slice, x[0,2] = ["a", "b", "c"]
. It will replace the slice with the content on the right. The slice [:]
means "from zero to last position". It's in place because it utilizes a generator instead of list comprehension. Because it's modified in place, if you use other references those will change as well:
>>> x = [ "Alice", "Bob", "Charlie", "Bob", "Dave" ]
>>> y = x
>>> x[:] = ( i for i in x if i != "Bob" )
>>> x
['Alice', 'Charlie', 'Dave']
>>> y
['Alice', 'Charlie', 'Dave']
While this removes the loop, it's not a function (as you asked). If you want a function you will have to extend the base type or define a new function def removeAll(list, symbol)
.