Removing item from list causes the list to become NoneType

Joseph_Renerr picture Joseph_Renerr · Nov 5, 2014 · Viewed 13k times · Source

I imagine there is a simple solution that I am overlooking. Better that than a complicated one, right?

Simply put:

var = ['p', 's', 'c', 'x', 'd'].remove('d')

causes var to be of type None. What is going on here?

Answer

Kevin picture Kevin · Nov 5, 2014

remove doesn't return anything. It modifies the existing list in-place. No assignment needed.

Replace

var = ['p', 's', 'c', 'x', 'd'].remove('d') 

with

var = ['p', 's', 'c', 'x', 'd']
var.remove('d') 

Now var will have a value of ['p', 's', 'c', 'x'].