Indexing Items in Python

The code snippet provided shows how to insert an item into a list at a specific position rather than appending it at the end.


Instead of adding "sunglasses" to the end of the list using append(), the task is to insert "sunglasses" at index 2 of the list travel_items.

    Explanation of the code:
    travel_items.insert(2, 'sunglasses')

    This uses the insert() method to add "sunglasses" at position 2. Index 2 refers to the third position in the list (since lists are zero-indexed).

    I will continue posting python lesson material, as it is always a good thing to refine and build new skills.

    Leave a comment