list.extend() vs list.insert()
list.extend() and list.insert() are both Python List Methods APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. list.extend(): Extends the list by appending all items from the iterable. Its syntax is list.extend(iterable), and it returns none list.insert(): Inserts an item at a given position. The first argument is the index. Its syntax is list.insert(index, x), and it returns none Example of list.extend(): lst = [1, 2]
lst.extend([3, 4])
# lst = [1, 2, 3, 4]. Example of list.insert(): lst = [1, 3]
lst.insert(1, 2)
# lst = [1, 2, 3]. Read the full references for list.extend() and list.insert() to see parameters, edge cases, and example code side by side. Other List Methods APIs worth comparing: list.append(), list.remove(), list.pop(), list.clear(), list.index(), list.count().