I create a list, and I want to remove a string from it.
Ex:
>>> myList = ['a', 'b', 'c', 'd']
>>> myList = myList.remove('c')
>>> print(myList)
None
What am I doing wrong here? All I want is 'c' to be removed from myList!
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With