Python List Methods: append, extend, insert, pop, sort
List Method Map
List methods change or inspect an ordered mutable collection.
Some methods mutate the list in place, while others return a value such as a removed item or an index.
The key list skill is knowing whether a method changes the same list or creates a separate result.
Add Items
append(), extend(), and insert() add values in different ways.
- append() adds one item at the end.
- extend() adds every item from another iterable.
- insert() places one item at a specific position.
Append vs Extend
Append vs Extend
items = ["pen"]
items.append("book")
items.extend(["bag", "scale"])
print(items)
Output
['pen', 'book', 'bag', 'scale']
append() added one string, while extend() added each item from the list.
Remove Items
remove(), pop(), clear(), and del remove list content in different ways.
- remove(value) deletes the first matching value.
- pop(index) removes and returns an item.
- clear() empties the list.
Pop the Last Task
Pop the Last Task
tasks = ["learn", "practice", "revise"]
last_task = tasks.pop()
print(last_task)
print(tasks)
Output
revise
['learn', 'practice']
pop() returns the removed item, which is useful when the removed value still matters.
Sort Lists
sort() changes the list in place. sorted() returns a new sorted list.
- Use sort() when changing the original list is fine.
- Use sorted() when you need to keep the original order too.
- Use key for custom sorting.
Copy Lists
copy() creates a shallow copy of a list.
- Use copy() when two names should not point to the same list.
- Nested lists still need deeper care because inner lists can still be shared.
-
Know which methods mutate the list in place.
-
Use append() for one item and extend() for many items.
-
Use pop() when you need the removed value.
-
Use sorted() when the original list must stay unchanged.
-
Use copy() before modifying a list that should remain separate.
List Mutation Traps
-
Assigning the result of append
append mutates the list and returns None, so call it on its own line.
-
Confusing sort and sorted
Use sort() to change the list in place and sorted() to create a new sorted list.
-
Removing while iterating
Build a new filtered list instead of removing items from the list you are looping over.
Try this next
Update a List Deliberately
0 of 3 completed
-
-
-
Questions About List Method
sort() changes the list in place and returns None to make that mutation obvious.
append() adds one object. extend() loops through another iterable and adds each item.
It creates a shallow copy. Inner lists are still shared unless you use a deeper copy strategy.