Python Lists
Python Lists: Ordering, Mutating, and Comprehensions
Python lists are ordered, mutable collections that hold items of any type inside square brackets []. They allow duplicates and keep items in the order you added them, which is why they are the default container for most everyday Python code.
This guide covers indexing, slicing, adding and removing items, sorting, list comprehensions, and the copying mistakes that trip up beginners most often.
How Python Lists Work
Each item has a zero-based index, so the first item is at 0, and the last is at -1. Because lists are mutable, you can change, add, or remove items after creating them, and the list grows or shrinks as needed. If you are new to the syntax, the official Python tutorial on lists is a good starting point. When you need a collection that should never change, compare lists with our lesson on Python tuples.
Modifying and Slicing Python Lists
Three methods cover most day-to-day changes:
append() adds one item to the end.
insert(index, item) places an item at a specific position.
pop() removes and returns an item, the last one by default.
tasks = ["lint", "test", "build"]
tasks.append("deploy")
tasks.insert(1, "typecheck")
done_task = tasks.pop()
print(tasks[:2]) # ['lint', 'typecheck']
print(done_task) # 'deploy'
The slice tasks[:2] returns a new list with the items at indices 0 and 1. The end index is never included. To verify any change, print the list afterwards. The list methods reference in the Python docs covers the rest.
append() vs. extend()
append() adds its argument as a single item. extend() takes any iterable and adds its items one by one.
a = [1, 2, 3]
a.append([4, 5])
print(a) # [1, 2, 3, [4, 5]]
b = [1, 2, 3]
b.extend([4, 5])
print(b) # [1, 2, 3, 4, 5]
If you see an unexpected nested list in your output, a misplaced append() is the usual cause.
List Comprehensions and Sorting
A list comprehension builds a new list in one expression, replacing the pattern of creating an empty list and filling it inside a for loop.
prices = [12, 45, 8, 30]
discounted = [round(p * 0.9, 2) for p in prices if p >= 10]
ordered = sorted(discounted, reverse=True)
print(ordered) # [40.5, 27.0, 10.8]Read it left to right: take each price p, keep it only if p >= 10, apply a 10% discount, and round to two decimals. If a comprehension becomes hard to read, switch back to a regular loop. Clarity matters more than brevity.
sort() vs. sorted()
items.sort() reorders the list in place and returns None. sorted(items) leaves the original untouched and returns a new sorted list. Choose sort() when you no longer need the original order, and sorted() when you do. The Python Sorting HOWTO shows how to sort with a key function.
Common Mistakes with Python Lists
Assigning
x = items.sort():.sort()modifiesitemsin place and returnsNone; usesorted(items)when you need a new list.Removing items while looping over the same list: Skipping elements happens when indices shift; filter with a comprehension instead.
Copying a list with
copy = original: That creates an alias; copy withoriginal.copy()orlist(original).
Frequently Asked Questions
Can a Python list hold mixed data types?
Yes. One list can store strings, integers, and objects together. Keeping items uniform makes processing simpler, though, so mix types only when your data genuinely needs it.
What is the difference between append() and extend()?
append([4, 5]) adds one nested list as a single item, while extend([4, 5]) adds 4 and 5 individually.
What is the difference between a list and a tuple?
Lists can be changed after creation; tuples cannot. See our lesson on Python tuples for when to prefer each.
When should I use a shallow copy vs. copy.deepcopy()?
Use items.copy() for flat lists of simple values. Use copy. deepcopy(items) when the list contains nested lists or dicts, so inner edits stay isolated.
Why does sort() print None?
Because it modifies the list in place and returns None. Print the list itself, or use sorted().
Conclusion
Python lists reward a few habits: know which methods change the list in place, build new lists with comprehensions instead of deleting mid-loop, and copy deliberately. Once these feel natural, continue with Python dictionaries to learn key-based storage, and practice in the exercises below.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.