The most important collection data type in Python is the list
data type. You’ll use lists basically in all your future projects so take 3-5 minutes and study this short guide carefully.
You can also play my short video tutorial as you read over the methods:
Method | Description |
---|---|
lst.append(x) | Appends element x to the list lst . |
| Removes all elements from the list lst –which becomes empty. |
lst.copy() | Returns a copy of the list lst . Copies only the list, not the elements in the list (shallow copy). |
lst.count(x) | Counts the number of occurrences of element x in the list lst . |
lst.extend(iter) | Adds all elements of an iterable iter (e.g. another list) to the list lst . |
lst.index(x) | Returns the position (index) of the first occurrence of value x in the list lst . |
lst.insert(i, x) | Inserts element x at position (index) i in the list lst . |
lst.pop() | Removes and returns the final element of the list lst . |
lst.remove(x) | Removes and returns the first occurrence of element x in the list lst . |
lst.reverse() | Reverses the order of elements in the list lst . |
lst.sort() | Sorts the elements in the list lst in ascending order. |
Let’s dive into a couple of examples:
l = [] l.append(2) print(l) l.clear() print(l) l.append(2) print(l) print(l.copy()) print(l.count(2)) l.extend([2,3,4]) print(l) print(l.index(3)) l.insert(2, 99) print(l) print(l.pop()) l.remove(2) print(l) l.reverse() print(l) l.sort() print(l)
Puzzle: Can you figure out all outputs of this interactive Python script?
Before you read on, first figure out the outputs. Then, compare them with the results here:
[2] [] [2] [2] 1 [2, 2, 3, 4] 2 [2, 2, 99, 3, 4] 4 [2, 99, 3] [3, 99, 2] [2, 3, 99]
If you’ve studied the table carefully, you’ll know the most important list methods in Python. Let’s have a look at some examples of above methods:
>>> l = [] >>> l.append(2) >>> l [2] >>> l.clear() >>> l [] >>> l.append(2) >>> l [2] >>> l.copy() [2] >>> l.count(2) 1 >>> l.extend([2,3,4]) >>> l [2, 2, 3, 4] >>> l.index(3) 2 >>> l.insert(2, 99) >>> l [2, 2, 99, 3, 4] >>> l.pop() 4 >>> l.remove(2) >>> l [2, 99, 3] >>> l.reverse() >>> l [3, 99, 2] >>> l.sort() >>> l [2, 3, 99]
Action Steps:
- Have a look at the ultimate guide of Python lists.
- Master Python Sets (with Harry Potter Examples)
- Master Python Dictionaries (the ultimate blog tutorial)
- Join my free email computer science academy for continuous improvement in Python
Here’s your free PDF cheat sheet showing you all Python list methods on one simple page. Click the image to download the high-resolution PDF file, print it, and post it to your office wall:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.