Python Forum

Full Version: Remove()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
After a successful remove() operation the list autosizes to one less element .Can we add a default value to the position from where the element was removed so that the length of list remains the same even after the element was removed?

Thanks
You'll need two lines
k = thelist.index(elt)
thelist[k] = "Spam"
or
thelist[thelist.index(elt)] = "Spam"
Just don't use list.remove() method for this
Instead, if you want to replace value in the list with "default" value

>>> spam = [1, 2, 3, 4]
>>> spam[2] = None # assuming None is "default" value
>>> spam
[1, 2, None, 4]
>>> spam[:2] = ['foo', 'bar']
>>> spam
['foo', 'bar', None, 4]