I create a list, and I want to remove a string from it.
Ex:
>>> myList = [\'a\', \'b\', \'c\', \'d\']
>>> myList = myList.remove
Remember that lists are mutable, so you can simply call remove
on the list itself:
>>> myList = ['a', 'b', 'c', 'd']
>>> myList.remove('c')
>>> myList
['a', 'b', 'd']
The reason you were getting None
before is because remove()
always returns None
Just an addition to Anand's Answer,
mylist = mylist.remove('c')
The above code will return 'none' as the return type for my list. So you want to keep it as
mylist.remove('c')
I am not sure what a
is (I am guessing another list), you should do myList.remove()
alone, without assignment.
Example -
>>> myList = ['a', 'b', 'c', 'd']
>>> myList.remove('c')
>>> myList
['a', 'b', 'd']
myList.remove()
does not return anything, hence when you do myList = <anotherList>.remove(<something>)
it sets myList to None
The remove()
function doesn't return anything, it modifies the list in place. If you don't assign it to a variable you will see that myList
doesn't contain c
anymore.