SyntaxStudy
Sign Up
Home Python Reference append() / extend() / insert()

append() / extend() / insert()

method

append() adds one item to the end; extend() adds all items from an iterable; insert() adds at a specific index.

Syntax

list.append(item) list.extend(iterable) list.insert(index, item)

Example

python
lst = [1, 2, 3]
lst.append(4)        # [1,2,3,4]
lst.extend([5, 6])   # [1,2,3,4,5,6]
lst.insert(0, 0)     # [0,1,2,3,4,5,6]

# Spread equivalent
lst2 = [*lst, 7, 8]  # [0,1,2,3,4,5,6,7,8]