Python List Methods
List Methods
Python lists come with built-in methods that let you add, remove, search, count, sort, and copy items. A method is a function that belongs to an object and is called with dot notation:
my_list.method_name(arguments)
This lesson is a practical reference for the eleven list methods, with an example, expected output, and explanation for each.
Before You Start: In-Place Methods Return None
Unlike string methods, most list methods change the list itself (they work in place) and return None. This is a very common source of bugs:
marks = [55, 88, 72]
result = marks.sort()
print(result)
print(marks)
Expected output:
None
[55, 72, 88]
sort() rearranged marks directly and returned nothing. Never write marks = marks.sort() — it would replace your list with None.
| Returns a Value | Modifies the List in Place (Returns None) |
|---|---|
copy(), count(), index(), pop() | append(), clear(), extend(), insert(), remove(), reverse(), sort() |
pop() is the only method that both modifies the list and returns a value.
Quick Reference
| Method | Description |
|---|---|
append(item) | Adds an item to the end of the list |
clear() | Removes all items |
copy() | Returns a shallow copy of the list |
count(value) | Returns how many times a value appears |
extend(iterable) | Adds every item from an iterable to the end |
index(value) | Returns the index of the first matching value |
insert(index, item) | Inserts an item at a given position |
pop(index) | Removes and returns the item at a position (last item by default) |
remove(value) | Removes the first matching value |
reverse() | Reverses the order of the items |
sort() | Sorts the items (ascending by default) |
1. append() — Add an Item to the End
Adds a single item to the end of the list.
tools = ["pen", "pencil"]
tools.append("eraser")
print(tools)
Expected output:
['pen', 'pencil', 'eraser']
append() always adds exactly one item. If you pass a list, the whole list becomes one nested item:
tools = ["pen", "pencil"]
tools.append(["ruler", "glue"])
print(tools)
Expected output:
['pen', 'pencil', ['ruler', 'glue']]
Use extend() (method 5) to add the items individually.
2. clear() — Remove All Items
Deletes every item, leaving an empty list. The list object itself still exists.
data = [10, 20, 30]
data.clear()
print(data)
Expected output:
[]
3. copy() — Create a Copy of the List
Returns a new list containing the same items.
colors = ["red", "green", "blue"]
backup = colors.copy()
colors.append("yellow")
print(colors)
print(backup)
Expected output:
['red', 'green', 'blue', 'yellow']
['red', 'green', 'blue']
Because backup is a separate list, adding "yellow" to colors does not affect it. Writing backup = colors instead would make both names refer to the same list.
Note:
copy()creates a shallow copy. If the list contains other lists, the inner lists are still shared. Usecopy.deepcopy()for nested data.
4. count() — Count Occurrences of a Value
Returns how many times a value appears in the list.
scores = [90, 80, 90, 70, 90]
print(scores.count(90))
Expected output:
3
If the value is not present, count() returns 0 — it does not raise an error.
5. extend() — Add Multiple Items
Adds each item from another iterable (list, tuple, set, string, and so on) to the end of the list.
list_a = [1, 2, 3]
list_b = [4, 5]
list_a.extend(list_b)
print(list_a)
Expected output:
[1, 2, 3, 4, 5]
list_a is modified; list_b is unchanged.
Be careful with strings — a string is an iterable of characters:
letters = ["a"]
letters.extend("bc")
print(letters)
Expected output:
['a', 'b', 'c']
6. index() — Find the Position of a Value
Returns the index of the first occurrence of a value.
languages = ["Python", "Java", "C++"]
print(languages.index("Java"))
Expected output:
1
If the value does not exist, index() raises a ValueError:
languages = ["Python", "Java", "C++"]
print(languages.index("Go"))
Output:
ValueError: 'Go' is not in list
Check with in first when the value might be missing:
if "Go" in languages:
print(languages.index("Go"))
else:
print("Go is not in the list")
Expected output:
Go is not in the list
7. insert() — Add an Item at a Specific Position
Inserts an item before the given index. Existing items from that position onward shift one place to the right.
numbers = [10, 30, 40]
numbers.insert(1, 20)
print(numbers)
Expected output:
[10, 20, 30, 40]
insert(0, item) adds an item at the beginning. If the index is larger than the list's length, the item is simply added at the end.
8. pop() — Remove an Item by Index
Removes the item at the given index and returns it.
queue = ["first", "second", "third"]
removed_item = queue.pop(1)
print(queue)
print(removed_item)
Expected output:
['first', 'third']
second
Without an index, pop() removes and returns the last item:
stack = ["page1", "page2", "page3"]
last_page = stack.pop()
print(last_page)
print(stack)
Expected output:
page3
['page1', 'page2']
This makes append() and pop() a natural pair for building a stack — a "last in, first out" structure, like a browser's back button history.
Calling pop() on an empty list raises IndexError: pop from empty list.
9. remove() — Remove an Item by Value
Removes the first item that matches the given value.
items = ["book", "pen", "pen", "notebook"]
items.remove("pen")
print(items)
Expected output:
['book', 'pen', 'notebook']
Only the first "pen" was removed. If the value is not found, remove() raises a ValueError.
To remove every occurrence, build a new list with a list comprehension:
items = ["book", "pen", "pen", "notebook"]
items = [item for item in items if item != "pen"]
print(items)
Expected output:
['book', 'notebook']
10. reverse() — Reverse the Order
Reverses the current order of the items in place. It does not sort them.
steps = [1, 2, 3, 4]
steps.reverse()
print(steps)
Expected output:
[4, 3, 2, 1]
If you need a reversed copy without changing the original, use slicing: steps[::-1].
11. sort() — Sort the List
Sorts the items in ascending order by default.
marks = [55, 88, 72, 60]
marks.sort()
print(marks)
Expected output:
[55, 60, 72, 88]
Optional Parameters
| Parameter | Purpose | Example |
|---|---|---|
reverse=True | Sort in descending order | marks.sort(reverse=True) |
key=function | Sort by a value computed from each item | names.sort(key=len) |
marks = [55, 88, 72, 60]
marks.sort(reverse=True)
print(marks)
names = ["Priya", "Al", "Kumar"]
names.sort(key=len)
print(names)
Expected output:
[88, 72, 60, 55]
['Al', 'Priya', 'Kumar']
key=len sorts the names by their length rather than alphabetically. Items with equal keys ("Priya" and "Kumar", both 5 letters) keep their original relative order.
Note: A list containing incompatible types, such as
[3, "a"], cannot be sorted and raises aTypeError.
Practical Example: Managing a To-Do List
This example combines several methods:
tasks = ["write report", "email team"]
tasks.append("review code") # Add a task to the end
tasks.insert(0, "check calendar") # Add an urgent task at the start
done = tasks.pop(1) # Complete the second task
print("Completed:", done)
print("Remaining:", tasks)
print("Number of tasks:", len(tasks))
tasks.sort()
print("Sorted:", tasks)
Expected output:
Completed: write report
Remaining: ['check calendar', 'email team', 'review code']
Number of tasks: 3
Sorted: ['check calendar', 'email team', 'review code']
Common Mistakes
| Mistake | Problem | Correct Approach |
|---|---|---|
items = items.sort() | items becomes None | Call items.sort() on its own line, or use sorted(items) |
items.append(1, 2) | TypeError: append() takes exactly one argument | items.extend([1, 2]) |
items.remove(x) for a missing value | ValueError | Check if x in items: first |
backup = items to make a copy | Both names refer to the same list | backup = items.copy() |
Related Concepts
- Python Lists — creating, accessing, and looping through lists
- Python Tuples — immutable sequences with only
count()andindex() - Built-in functions such as
len(),sorted(),min(),max(), andsum(), which also work with lists