Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove()
#1
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
Reply
#2
You'll need two lines
k = thelist.index(elt)
thelist[k] = "Spam"
or
thelist[thelist.index(elt)] = "Spam"
Reply
#3
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]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020