List Methods Examples
Short, runnable programs that show python list methods in action. Read the explanation, press Run, then change the code and try your own ideas. New to the topic? Start with the Python List Methods lesson.
1append, extend or +: three ways to combine lists
Adding one list to another gives three different results. append() nests it, extend() merges it in place, and + makes a new list.
morning = ["email", "standup"]
afternoon = ["review", "deploy"]
as_one_item = morning.copy()
as_one_item.append(afternoon)
print(as_one_item)
merged = morning.copy()
merged.extend(afternoon)
print(merged)
combined = morning + afternoon
print(combined)
print(morning)Expected output['email', 'standup', ['review', 'deploy']]
['email', 'standup', 'review', 'deploy']
['email', 'standup', 'review', 'deploy']
['email', 'standup']
2A browser history as a stack
A stack is last-in, first-out. append() pushes a page and pop() takes the newest one back off, like the Back button.
history = ["home"]
for page in ["search", "product", "cart"]:
history.append(page)
print("Visited", page, "->", history)
while len(history) > 1:
leaving = history.pop()
print("Back from", leaving, "to", history[-1])Expected outputVisited search -> ['home', 'search']
Visited product -> ['home', 'search', 'product']
Visited cart -> ['home', 'search', 'product', 'cart']
Back from cart to product
Back from product to search
Back from search to home
3A print queue served from the front
A queue is first-in, first-out, so jobs are taken from position 0. The loop runs while the list is not empty (an empty list counts as false).
jobs = ["report.pdf", "photo.png", "notes.txt"]
while jobs:
current = jobs.pop(0)
print("Printing", current, "-", len(jobs), "left")
print("Queue empty:", jobs)Expected outputPrinting report.pdf - 2 left
Printing photo.png - 1 left
Printing notes.txt - 0 left
Queue empty: []
4remove() deletes only the first match
When a value appears several times, one remove() takes out one copy. Loop with in to clear them all.
tags = ["sale", "new", "sale", "sale", "hot"]
print("sale appears", tags.count("sale"), "times")
tags.remove("sale")
print(tags)
while "sale" in tags:
tags.remove("sale")
print(tags)Expected outputsale appears 3 times
['new', 'sale', 'sale', 'hot']
['new', 'hot']
5Finding positions with index() and count()
index() accepts a second argument, the position to start searching from. That lets you find the second match, and the third, and so on.
levels = ["INFO", "ERROR", "INFO", "WARN", "ERROR", "INFO"]
print("errors:", levels.count("ERROR"))
first = levels.index("ERROR")
second = levels.index("ERROR", first + 1)
print("first error at", first, "second at", second)
print("no CRITICAL:", levels.count("CRITICAL") == 0)Expected outputerrors: 2
first error at 1 second at 4
no CRITICAL: True
6Edit a copy and keep the original
copy() gives you a separate list, so you can experiment with reverse(), insert() and pop() while the original stays exactly as it was.
original = ["red", "green", "blue"]
draft = original.copy()
draft.reverse()
draft.insert(1, "yellow")
draft.pop()
print("original:", original)
print("draft:", draft)Expected outputoriginal: ['red', 'green', 'blue']
draft: ['blue', 'yellow', 'green']
Practise next
Python List Methods exercises → Back to the lesson
Python Lists examplesPython Slicing examplesPython Tuples examplesPython Sorting examples