What’s the runtime complexity of various list methods?
The following table summarizes the runtime complexity of all list methods.
Assume that the length of the data type is defined as n
(that is—len(data_type)
). You can now categorize the asymptotic complexity of the different complexity functions as follows:
Operation | Example | Complexity |
---|---|---|
Index | l[i] | O(1) |
Store | l[i] = 42 | O(1) |
Length | len(l) | O(1) |
Append | l.append(42) | O(1) |
Pop | l.pop() | O(1) |
Clear | l.clear() | O(1) |
Slicing | l[a:b] | O(b-a) |
Extend | l1.extend(l2) | O(len(l1)+len(l2)) |
Constructor | list(iter) | O(len(iter)) |
Equality | l1 == l2 | O(n) |
Slice Assign | l[a:b] = ... | O(n) |
Delete | del l[i] | O(n) |
Remove | l.remove(...) | O(n) |
Membership | x in l / x not in l | O(n) |
Copy | l.copy() | O(n) |
Pop | l.pop(0) | O(n) |
Min | min(l) | O(n) |
Max | max(l) | O(n) |
Reverse | l.reverse() | O(n) |
Iterate | for x in l: | O(n) |
Sort | l.sort() | O(n log(n)) |
Multiply | l*k | O(n k) |
Need to learn more about these methods? Watch me giving you a quick overview of all Python list methods:
You can read more about all methods in my detailed tutorial on the Finxter blog.
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:
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.