Module 3 · Lists and list processing · Lesson 4 of 7
Adding elements
Lists grow. The everyday way is append(), which adds a value to the end — quick, and the list simply gets one longer. But sometimes you need to drop a value into the middle, and that's insert().
nums.insert(1, 15)append(x) is the easy cousin — it just drops a value on the end, no shifting required.
nums.insert(1, 15) means “put 15 at index 1.” To make room, every element from that position on slides one place to the right — the list reshuffles itself around the newcomer. append(x) is really just the special case of inserting at the very end, where nothing has to move.
That last snippet is one of the most common patterns in all of programming: start with an empty list and append to it inside a loop, building the result up one item at a time. You'll write it again and again.