Append Items
To add an item to the end of a list in Python, the append() method is used. This method allows you to insert a single element as the last item of the list without affecting the existing elements. It is especially useful when you need to build a list dynamically by adding items one at a time. For example, if you have a list of fruits and want to add “mango” at the end, you can simply use fruits.append("mango"). This operation modifies the original list in place and is one of the most commonly used methods for managing list data.
Do it
Insert Items
To insert a list item at a specified index in Python, you can use the insert() method. This method allows you to place a new element at any position within the list, shifting the existing elements to the right. The insert() method takes two arguments: the index at which to insert the item and the item itself. For example, my_list.insert(2, "grape") will insert “grape” at index 2, pushing the current and subsequent items one position forward. This is useful when you need to maintain a specific order or priority within your list.