Python - Change List Items
Python - Change List Items
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python HOME
Python Intro
Python Get Started
Join Lists
Example
Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
Try it Yourself »
If you insert more items than you replace, the new items will be inserted where you specified, and the remaining items will move
accordingly:
Example
Change the second value by replacing it with two new values:
Try it Yourself »
Note: The length of the list will change when the number of items inserted does not match the number of items replaced.
If you insert less items than you replace, the new items will be inserted where you specified, and the remaining items will move
accordingly:
Example
Change the second and third value by replacing it with one value:
Try it Yourself »
ADVERTISEMENT
Insert Items
To insert a new list item, without replacing any of the existing values, we can use the insert() method.
Example
Insert "watermelon" as the third item:
Try it Yourself »
Note: As a result of the example above, the list will now contain 4 items.
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.