Loading section...

Adding Items to Lists

Concepts: pyListModify

append(): Adding to the End Building Lists with Loops insert(): Specific Position When we insert "b" at index 1, the items "c" and "d" shift to the right to make room. What was at index 1 ("c") moves to index 2. What was at index 2 ("d") moves to index 3. The new item "b" takes position 1. Inserting at the Beginning To insert at the very beginning of a list, use index 0. This pushes all existing items one position to the right. Every existing item shifts right by one position. "second" moves from index 0 to index 1, "third" from 1 to 2, and "fourth" from 2 to 3. The new item "first" takes index 0. insert() Edge Cases What happens if you try to insert at an index beyond the list's current length? Python handles this gracefully by inserting at the end.