Modifying A List
Modifying a list in Python
Lists are mutable data types meaning you can change the state of a list after it has been created. Adding items, removing items, or changing items.
1. Modifying a List Item
Changing a List item
indexing
2. Adding a List item
using append()
to add an item
using insert()
method
List insert()
method takes two arguments:
- index: An index on which an item should be inserted
- item: An item to be inserted at that specific position
Note:
Insert an item at that particular index or position, if there’s an element at that position then it pushes that element forward
3. Removing a List item
using pop()
pop()
removes an item at the last position or index in the list.
using remove()
remove()
method removes a given list item from the list.