List Methods & Slicing in Python
Lists support a wide set of built-in methods for adding, removing, sorting, and rearranging items, plus the same slicing syntax used on strings for extracting a sub-list by position. Because lists are mutable, many of these methods change the list directly rather than returning a new one — worth watching closely, since it's a common source of surprises.
Slicing works the same as strings
list[start:stop:step] extracts a range of items.
Adding methods modify in place
.append(), .insert(), and .extend() all change the original list and return None.
Removing methods modify in place too
.remove() and .pop() change the original — .pop() additionally returns the removed item.
.sort() vs sorted()
.sort() rearranges the list in place; the built-in sorted() returns a new sorted list, leaving the original untouched.
.copy() avoids shared references
Creates a genuinely separate list, rather than a second name pointing at the same one.
nums = [3, 1, 4, 1, 5]print(nums[1:4]) nums.append(9)print(nums) nums.sort()print(nums)Adding Items
.append() adds one item to the end; .insert() adds at a specific position; .extend() adds every item from another iterable.
nums = [1, 2]nums.insert(1, 99)print(nums)Removing Items
.remove() deletes the first matching value; .pop() removes and returns an item by index (the last one, by default).
nums = [1, 2, 3]last = nums.pop()print(last, nums)sort() vs sorted()
.sort() rearranges the list in place and returns None; the built-in sorted() leaves the original untouched and returns a brand new sorted list.
nums = [3, 1, 2]result = sorted(nums)print(result, nums)Copying a List
Assigning with = just creates a second name pointing at the same list; .copy() (or list(original)) creates a genuinely separate list.
original = [1, 2, 3]copy = original.copy()copy.append(4)print(original, copy)append() is fast because it just adds to the end, but insert() and remove() may need to shift every following item over by one position, making them O(n) in the worst case.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.